authorize.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. <template>
  2. <view>
  3. <!--pages/authorize/authorize.wxml-->
  4. <block v-if="isLong">
  5. <view class="about">
  6. <view class="logo">
  7. <image :src="logo"></image>
  8. <view>{{ name }}</view>
  9. </view>
  10. </view>
  11. <view class="btn">
  12. <button class="login" @click="getWXUserProfile()">授权登录</button>
  13. <!-- <button class="login" open-type="getUserInfo" @getuserinfo="bindGetUserInfo">授权登录</button> -->
  14. <view class="zanbu" @tap="back">暂不登录</view>
  15. <view hover-class="none" class="xieyi ddflex">
  16. <view @click="jump('/mine/page/page?title=用户协议&isXieyi=true')">《用户协议》</view>
  17. <view @click="jump('/mine/page/page?title=隐私声明&isYinsi=true')">《隐私声明》</view>
  18. </view>
  19. <view class="copy" v-if="about.name && about.recordNumber">{{ about.name ? about.name : '' }}版权所有© {{ about.recordNumber ? about.recordNumber : '' }}</view>
  20. </view>
  21. </block>
  22. <block v-if="isphone">
  23. <view class="phone">
  24. <view class="logo">
  25. <image :src="logo"></image>
  26. <view>{{ name }}</view>
  27. </view>
  28. </view>
  29. <view class="btn">
  30. <button class="btnphone" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">微信手机号快捷绑定</button>
  31. <!-- <view class="btngophone r10">手机号验证绑定</view> -->
  32. <view class="zanbu" @tap="back">暂不绑定</view>
  33. <view hover-class="none" class="xieyi ddflex">
  34. <view @click="jump('/mine/page/page?title=用户协议&isXieyi=true')">《用户协议》</view>
  35. <view @click="jump('/mine/page/page?title=隐私声明&isYinsi=true')">《隐私声明》</view>
  36. </view>
  37. <view class="copy" v-if="about.name && about.recordNumber">{{ about.name ? about.name : '' }}版权所有© {{ about.recordNumber ? about.recordNumber : '' }}</view>
  38. </view>
  39. </block>
  40. <view class="ceng" v-if="isXyShow"></view>
  41. <view class="pop-protocol" :style="'bottom:' + XyBottom">
  42. <view class="pop-tit">个人隐私保护指引</view>
  43. <view class="pop-con">
  44. 为更好的保障你的个人信息安全及权利行使。我们将通过
  45. <text @click="jump('/mine/page/page?title=用户协议&isXieyi=true')">《用户协议》</text>
  46. <text @click="jump('/mine/page/page?title=隐私声明&isYinsi=true')">《隐私声明》</text>
  47. 帮助你了解我们收集、使用、存储、公开披露个人信息的情况。请仔细阅读并理解本政策的内容,若你同意,请点击下方按钮开始接受我们的服务。
  48. </view>
  49. <view class="pop-btn">
  50. <view class="pop-agree" @click="agn()">同意并继续</view>
  51. <view class="pop-no" @click="back()">不同意并退出</view>
  52. </view>
  53. </view>
  54. <!-- <privacy :is-bindMobile="false" :is-timeout="false"></privacy> -->
  55. </view>
  56. </template>
  57. <script>
  58. // pages/authorize/authorize.js
  59. const app = getApp();
  60. const req = require('../../utils/request.js');
  61. export default {
  62. data() {
  63. return {
  64. sessionKey: '',
  65. openid: '',
  66. about: {},
  67. isShow: true,
  68. isphone: false,
  69. isLong: true,
  70. istetphone: 0,
  71. config: '',
  72. logo: '',
  73. name: '',
  74. isPromote: false, //是否从分销进入
  75. isXyShow: true, //默认显示协议指引
  76. XyBottom: '-100%'
  77. };
  78. },
  79. components: {},
  80. props: {},
  81. onLoad: function(options) {
  82. req.removeStorage('authorize_jump')
  83. var agnXY = req.getStorage('agnXY');
  84. if (agnXY) {
  85. this.isXyShow = false;
  86. this.XyBottom = '-100%';
  87. } else {
  88. setTimeout(res => {
  89. this.XyBottom = 0;
  90. }, 200);
  91. }
  92. app.globalData.getCheckSessoin(json => {
  93. this.sessionKey = json.session_key;
  94. this.openid = json.openid;
  95. });
  96. console.log('登录页面===********************' + JSON.stringify(options));
  97. if (req.getStorage('configRes')) {
  98. let configRes = JSON.parse(req.getStorage('configRes'));
  99. this.setData({
  100. istetphone: configRes.user_bind_mobile,
  101. logo: configRes.CONFIG_PROJECT_LOGO,
  102. name: configRes.CONFIG_PROJECT_TITLE,
  103. config: configRes
  104. });
  105. } else {
  106. req.g('/api/config', res => {
  107. req.setStorage('configRes', JSON.stringify(res));
  108. this.setData({
  109. istetphone: res.user_bind_mobile,
  110. logo: res.CONFIG_PROJECT_LOGO,
  111. name: res.CONFIG_PROJECT_TITLE,
  112. config: res
  113. });
  114. });
  115. }
  116. if (options.bindmobile == 1) {
  117. this.isphoneFun();
  118. }
  119. if (options.scene) {
  120. this.scene = options.scene;
  121. this.loadCodeParams();
  122. }
  123. this.getAbout();
  124. // this.isphone()
  125. },
  126. onShow: function() {
  127. uni.setNavigationBarColor({
  128. frontColor: '#000000',
  129. backgroundColor: '#FFFFFF'
  130. });
  131. uni.setNavigationBarTitle({
  132. title: '授权登录'
  133. }); // this.isshop()
  134. },
  135. onUnload: function() {
  136. // 调用登录的时候会存一个REDIRECT_URL 根据REDIRECT_URL来判断用户是从哪里登录,identification用户点击暂不绑定的标识
  137. // console.log(this.identification,req.getStorage('REDIRECT_URL'),app)
  138. if (req.getStorage('REDIRECT_URL') == '/pages/user/user' && !this.identification) {
  139. // 判断是否开启了强制绑定手机号,并且判断缓存是否有userInfo,是否存在userInfo.mobile电话
  140. if (this.istetphone == '1' && req.getStorage('userInfo') && !req.getStorage('userInfo').mobile) {
  141. let pages = getCurrentPages();
  142. var prevPage = pages[pages.length - 2];
  143. prevPage.$vm.setData({
  144. noLogin: true
  145. });
  146. uni.navigateBack({
  147. delta: -1
  148. });
  149. // app.globalData.switchTab('pages/index/index')
  150. }
  151. // 判断用户是否登录,点击返回键,就跳转到首页,并传个值到user页面,阻止调用接口,防止登录
  152. if (!req.getStorage('userInfo')) {
  153. let pages = getCurrentPages();
  154. var prevPage = pages[pages.length - 2];
  155. prevPage.$vm.setData({
  156. noLogin: true
  157. });
  158. uni.navigateBack({
  159. delta: -1
  160. });
  161. // app.globalData.switchTab('pages/index/index')
  162. }
  163. }
  164. },
  165. methods: {
  166. jump(url) {
  167. uni.navigateTo({
  168. url: url
  169. });
  170. },
  171. loadCodeParams() {
  172. let _ts = this;
  173. return new Promise((resolve, reject) => {
  174. if (!_ts.scene) {
  175. resolve();
  176. return false;
  177. }
  178. req.getRequest(
  179. '/api/code/params',
  180. {
  181. scene: _ts.scene
  182. },
  183. data => {
  184. if (data.scene.split('_')[1] == 'isPromote') {
  185. this.isPromote = true;
  186. }
  187. // console.log("获取用户id====++++++++++++++" + JSON.stringify(data));
  188. req.setStorage('pidCode', data.userId);
  189. resolve();
  190. }
  191. );
  192. });
  193. },
  194. agn() {
  195. this.isXyShow = false;
  196. this.XyBottom = '-100%';
  197. req.setStorage('agnXY', true);
  198. },
  199. back() {
  200. this.XyBottom = '-100%';
  201. this.identification = true;
  202. // uni.switchTab({
  203. // url: req.header.appId=='ZQ1VK5oc17I387E'?'/pages/timelyDelivery/index':'/pages/index/index'
  204. // });
  205. if (this.isPromote) {
  206. app.globalData.reLaunchTo('pages/index/index')
  207. } else {
  208. uni.navigateBack();
  209. }
  210. },
  211. getAbout() {
  212. var _this = this;
  213. req.getRequest(
  214. '/api/aboutAs',
  215. {},
  216. function(res) {
  217. _this.setData({
  218. about: res
  219. });
  220. },
  221. true
  222. );
  223. },
  224. getWXUserProfile() {
  225. let that = this;
  226. // #ifdef MP-WEIXIN
  227. try {
  228. uni.getUserProfile({
  229. desc: '注册',
  230. success: res => {
  231. // console.log('数据====', JSON.stringify(res));
  232. that.toSubmitLogin(res.iv, res.encryptedData);
  233. },
  234. fail: res => {
  235. // console.log('数据====', res);
  236. req.msg(res);
  237. }
  238. });
  239. } catch (e) {
  240. // 老版
  241. wx.getUserInfo({
  242. success: function(ress) {
  243. // console.log('getUserInfo====', JSON.stringify(ress));
  244. that.toSubmitLogin(ress.iv, ress.encryptedData);
  245. }
  246. });
  247. }
  248. // #endif
  249. // #ifdef MP-TOUTIAO
  250. uni.login({
  251. success(res) {
  252. console.log('静默授权登录>>>', res);
  253. let params = {
  254. code: res.code,
  255. parentId: req.getStorage('pidCode')
  256. };
  257. return new Promise((resolve, reject) => {
  258. req.postRequest(
  259. '/api/v3/tiktok/login',
  260. params,
  261. data => {
  262. if (data.token) {
  263. req.setStorage('AUTH_TOKEN', data.token);
  264. }
  265. req.setStorage('userInfo', data);
  266. if (that.istetphone != '1') {
  267. uni.navigateBack();
  268. } else {
  269. if (data.mobile) {
  270. uni.navigateBack();
  271. } else {
  272. that.isphoneFun();
  273. }
  274. }
  275. },
  276. true
  277. );
  278. });
  279. }
  280. });
  281. // #endif
  282. },
  283. toSubmitLogin(iv, encryptedData) {
  284. let that = this;
  285. if (!encryptedData || !iv) return false;
  286. let _params = {
  287. sessionKey: this.sessionKey,
  288. openId: this.openid,
  289. iv: iv,
  290. encryptedData: encryptedData
  291. };
  292. if (req.getStorage('pidCode')) {
  293. _params.parentId = req.getStorage('pidCode');
  294. }
  295. req.postRequest('/api/login', _params, json => {
  296. let params = json;
  297. // console.log(json);
  298. that.setData(params);
  299. let url = req.getStorage('REDIRECT_URL');
  300. req.setStorage('userInfo', json);
  301. // req.removeStorage('MERCHANT')
  302. if (that.scene) {
  303. that.back();
  304. } else {
  305. // console.log(json.mobile);
  306. if (that.istetphone != '1') {
  307. uni.navigateBack();
  308. } else {
  309. if (json.mobile) {
  310. uni.navigateBack();
  311. } else {
  312. that.isphoneFun();
  313. }
  314. }
  315. }
  316. that.isshop();
  317. });
  318. },
  319. //原来的登录
  320. bindGetUserInfo(e) {
  321. var that = this;
  322. let data = e.detail;
  323. if (!data.encryptedData || !data.iv) return false;
  324. let _params = {
  325. sessionKey: this.sessionKey,
  326. iv: data.iv,
  327. encryptedData: data.encryptedData
  328. }; // console.log("!!!!!!!!!!!!!!!!!!!!!!"+_params)
  329. if (req.getStorage('pidCode')) {
  330. _params.parentId = req.getStorage('pidCode');
  331. }
  332. // console.log("登录页面===" + JSON.stringify(_params));
  333. req.postRequest('/api/login', _params, json => {
  334. let params = json;
  335. // console.log(json);
  336. that.setData(params);
  337. let url = req.getStorage('REDIRECT_URL');
  338. req.setStorage('userInfo', json);
  339. // req.removeStorage('MERCHANT')
  340. if (that.scene) {
  341. that.back();
  342. } else {
  343. // console.log(json.mobile);
  344. if (that.istetphone != '1') {
  345. uni.navigateBack();
  346. } else {
  347. if (json.mobile) {
  348. uni.navigateBack();
  349. } else {
  350. that.isphoneFun();
  351. }
  352. }
  353. }
  354. that.isshop();
  355. });
  356. },
  357. //判断是否开启手机绑定
  358. isphoneFun() {
  359. this.setData({
  360. isphone: true,
  361. isLong: false
  362. });
  363. uni.setNavigationBarTitle({
  364. title: '绑定手机号'
  365. });
  366. },
  367. getPhoneNumber(e) {
  368. let that = this;
  369. let _params = {
  370. sessionKey: that.sessionKey,
  371. iv: e.detail.iv,
  372. encryptedData: e.detail.encryptedData
  373. };
  374. // console.log(e.detail.errMsg);
  375. if (req.getStorage('pidCode')) {
  376. _params.parentId = req.getStorage('pidCode');
  377. }
  378. if (e.detail.errMsg == 'getPhoneNumber:ok') {
  379. req.postRequest('/api/weixin/mobile', _params, json => {
  380. // console.log(json);
  381. if (json.mobile) {
  382. var userInfo = req.getStorage('userInfo');
  383. userInfo.mobile = json.mobile;
  384. req.setStorage('userInfo', userInfo);
  385. if (that.scene) {
  386. that.back();
  387. } else {
  388. uni.navigateBack();
  389. }
  390. }
  391. });
  392. } else {
  393. }
  394. },
  395. // 分销的配置
  396. isshop() {
  397. req.p('/api/distribution/config', data => {
  398. // console.log(data);
  399. req.setStorage('distribution', data);
  400. });
  401. }
  402. }
  403. };
  404. </script>
  405. <style>
  406. @import './authorize.css';
  407. </style>