wxLogin.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. <template>
  2. <view class="content">
  3. <!-- #ifndef H5 -->
  4. <image class="bg-img" :src="baseURL + urlFile + '/img/img09.png'" mode=" scaleToFill"></image>
  5. <view class="logo-img-box">
  6. <image class="logo-img" src="https://zhibo.liuniu946.com/img/logo001.png" mode="aspectFit"></image>
  7. <button class="userInfo" type="warn" @click="getUserProfile">
  8. <text class="iconfont iconweixin"></text>
  9. <text @click="clickSq">微信授权登录</text>
  10. </button>
  11. </view>
  12. <!-- #endif -->
  13. <view class="Mask" v-show="MaskShow">
  14. <view class="Mask-box">
  15. <view class="title">申请获取您的手机号</view>
  16. <view class="text">为了方便您的取货和我们的送货,并获取更多优惠活动,需要您的手机授权</view>
  17. <view class="btn_box">
  18. <button class="weixin" @click="ToIndex()">取消</button>
  19. <button class="weixin" open-type="getPhoneNumber" @getphonenumber="PhoneNumber">手机号授权</button>
  20. </view>
  21. </view>
  22. </view>
  23. </view>
  24. </template>
  25. <script>
  26. // #ifdef H5
  27. import { loginWinxin } from '@/utils/wxAuthorized';
  28. // #endif
  29. // #ifdef MP-WEIXIN
  30. import { loginWinxinMp } from '@/utils/wxMinProgram';
  31. import { wechatMpAuth } from '@/api/wx';
  32. // #endif
  33. import { logout } from '@/api/set.js';
  34. import { mapMutations, mapState } from 'vuex';
  35. import { getUserInfo, bangding } from '@/api/login.js';
  36. export default {
  37. data() {
  38. return {
  39. userInfo: {}, //授权用户信息
  40. code: '', //授权code
  41. loding: false, //判断是否在点击中
  42. MaskShow: false // 手机号授权弹窗
  43. };
  44. },
  45. onLoad(option) {
  46. this.loadData();
  47. },
  48. computed: {
  49. ...mapState(['baseURL', 'urlFile'])
  50. },
  51. methods: {
  52. ...mapMutations(['login', 'setUserInfo']),
  53. clickSq() {
  54. console.log('授权');
  55. },
  56. loadData() {
  57. // #ifdef H5
  58. loginWinxin();
  59. // #endif
  60. // #ifdef MP
  61. loginWinxinMp();
  62. // #endif
  63. },
  64. // #ifndef H5
  65. // 用户确认授权
  66. getUserProfile(e) {
  67. const that = this;
  68. // #ifdef MP-WEIXIN
  69. if (!this.loding) {
  70. wx.getUserProfile({
  71. desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  72. success: res => {
  73. that.userInfo = res;
  74. if(!!res.iv){
  75. that.loadMp(res);
  76. }else{
  77. uni.showModal({
  78. title: '问题',
  79. content: '您的微信版本过低请升级微信',
  80. showCancel: false,
  81. });
  82. }
  83. },
  84. fail: err => {
  85. console.log('getUserProfile出错', err);
  86. }
  87. });
  88. }
  89. // #endif
  90. },
  91. // #endif
  92. // #ifdef MP-WEIXIN
  93. loadMp(user) {
  94. let obj = this;
  95. obj.loding = true;
  96. // 获取推广人id
  97. let spread_spid = uni.getStorageSync('spread') || '';
  98. let spread_code = uni.getStorageSync('spread_code') || '';
  99. uni.showLoading({
  100. title: '授权中',
  101. mask: true
  102. });
  103. wx.login({
  104. success(e) {
  105. console.log('wx.login',user)
  106. let pushData = {
  107. code: e.code,
  108. iv: user.iv,
  109. encryptedData: user.encryptedData,
  110. spread_spid: spread_spid,
  111. spread_code: spread_code
  112. };
  113. wechatMpAuth(pushData)
  114. .then(({ data }) => {
  115. obj.loding = false;
  116. obj.wchatAuth(data);
  117. console.log(data);
  118. })
  119. .catch(e => {
  120. obj.loding = false;
  121. });
  122. },
  123. fill: function(e) {
  124. obj.loding = false;
  125. console.log(e);
  126. }
  127. });
  128. },
  129. wchatAuth(data) {
  130. let obj = this;
  131. // 保存token
  132. uni.setStorageSync('token', data.token);
  133. // 获取用户基础信息
  134. getUserInfo({})
  135. .then(e => {
  136. console.log('--获取用户基础信息getUserInfo--', e);
  137. uni.hideLoading();
  138. obj.login();
  139. // 保存返回用户数据
  140. obj.setUserInfo(e.data);
  141. obj.ToIndex();
  142. })
  143. .catch(e => {
  144. uni.hideLoading();
  145. });
  146. },
  147. // #endif
  148. // #ifdef MP-WEIXIN
  149. // loadMp(option) {
  150. // let obj = this;
  151. // obj.loding = true;
  152. // // 获取登录授权页数据
  153. // let user = obj.userInfo;
  154. // // 获取推广人id
  155. // let spread_spid = uni.getStorageSync('spread') || '';
  156. // // uni.showLoading({
  157. // // title: '授权中',
  158. // // mask: true
  159. // // });
  160. // loginWinxinMp().then(e => {
  161. // this.code = e.code;
  162. // wechatMpAuth({
  163. // code: this.code,
  164. // iv: user.target.iv,
  165. // encryptedData: user.target.encryptedData,
  166. // spread_spid: spread_spid
  167. // })
  168. // .then(({
  169. // data
  170. // }) => {
  171. // uni.showModal({
  172. // title: '用户确认授权wechatMpAuth',
  173. // content: JSON.stringify(data)
  174. // })
  175. // obj.login();
  176. // obj.loding = false;
  177. // obj.wchatAuth(data);
  178. // uni.hideLoading();
  179. // })
  180. // .catch(e => {
  181. // uni.showModal({
  182. // title: '用户确认授权wechatMpAuth失败',
  183. // content: JSON.stringify(e)
  184. // })
  185. // obj.loding = false;
  186. // uni.hideLoading();
  187. // });
  188. // });
  189. // },
  190. // wchatAuth(data) {
  191. // let obj = this;
  192. //
  193. // // 保存token
  194. // uni.setStorageSync('token', data.token);
  195. // // 获取用户基础信息
  196. // getUserInfo({})
  197. // .then(e => {
  198. // uni.showModal({
  199. // title:'获取用户基础信息',
  200. // content:JSON.stringify(e)
  201. // })
  202. // uni.hideLoading();
  203. // obj.login();
  204. // // 保存返回用户数据
  205. // obj.setUserInfo(e.data);
  206. // if (e.data.status == 0) {
  207. // uni.showModal({
  208. // title: '提示',
  209. // content: '您的账号存在异常,暂时无法登陆',
  210. // showCancel: true,
  211. // success() {
  212. // obj.toLogout();
  213. // }
  214. // })
  215. // } else {
  216. // obj.ToIndex();
  217. // }
  218. // // obj.ToIndex();
  219. // // if (e.data.phone == null || !e.data.phone ) {
  220. // // obj.MaskShow = true;
  221. // // } else {
  222. // // obj.ToIndex();
  223. // // }
  224. // })
  225. // .catch(e => {
  226. // uni.hideLoading();
  227. // });
  228. // },
  229. // #endif
  230. toLogout() {
  231. logout({})
  232. .then(e => {
  233. uni.navigateBack();
  234. })
  235. .catch(e => {
  236. console.log(e);
  237. });
  238. },
  239. ToIndex() {
  240. let obj = this;
  241. let ur = uni.getStorageSync('present') || '/pages/index/index';
  242. console.log('---登录跳转---', ur);
  243. // 用于处理缓存bug
  244. if (ur == 'pages/product/product') {
  245. ur = '/pages/index/index';
  246. }
  247. uni.switchTab({
  248. url: ur,
  249. fail(e) {
  250. uni.navigateTo({
  251. url: ur,
  252. fail(e) {
  253. uni.navigateTo({
  254. url: '/pages/index/index'
  255. });
  256. }
  257. });
  258. }
  259. });
  260. },
  261. // 绑定手机号
  262. PhoneNumber(e) {
  263. let obj = this;
  264. obj.MaskShow = false;
  265. (obj.iv = e.detail.iv), (obj.encryptedData = e.detail.encryptedData);
  266. uni.setStorageSync('code', obj.code);
  267. bangding({
  268. flag: 1,
  269. cache_key: obj.cache_key,
  270. code: obj.code,
  271. iv: obj.iv,
  272. encryptedData: obj.encryptedData
  273. }).then(function(e) {
  274. if (e.data.is_bind == 1) {
  275. console.log('bangding1');
  276. bangding({
  277. flag: 1,
  278. cache_key: obj.cache_key,
  279. code: obj.code,
  280. iv: obj.iv,
  281. encryptedData: obj.encryptedData,
  282. step: 1
  283. })
  284. .then(function(e) {
  285. // 获取用户基础信息
  286. obj.GetUser();
  287. obj.$api.msg(e.msg);
  288. obj.$nextTick(function() {
  289. obj.ToIndex();
  290. });
  291. })
  292. .catch(e => {
  293. console.log(e);
  294. });
  295. } else {
  296. console.log('bangding2');
  297. obj.$api.msg(e.msg);
  298. // 获取用户基础信息
  299. obj.GetUser();
  300. obj.$api.msg(e.msg);
  301. obj.$nextTick(function() {
  302. obj.ToIndex();
  303. });
  304. }
  305. });
  306. },
  307. GetUser() {
  308. // 获取用户基础信息
  309. getUserInfo({})
  310. .then(({ data }) => {
  311. this.setUserInfo(data);
  312. console.log(data, 11);
  313. console.log(uni.getStorageSync('userInfo'), 55);
  314. })
  315. .catch(e => {
  316. console.log(e);
  317. });
  318. }
  319. }
  320. };
  321. </script>
  322. <style lang="scss">
  323. page,
  324. .content {
  325. height: 100%;
  326. background: #ffffff;
  327. }
  328. .Mask {
  329. position: fixed;
  330. top: 0;
  331. width: 100%;
  332. height: 100%;
  333. background-color: rgba(51, 51, 51, 0.7);
  334. .Mask-box {
  335. margin: auto;
  336. margin-top: 320rpx;
  337. padding: 50rpx 30rpx;
  338. width: 90%;
  339. height: 450rpx;
  340. top: 500rpx;
  341. left: 10%;
  342. background-color: #ffffff;
  343. border-radius: 15rpx;
  344. .title {
  345. text-align: center;
  346. font-size: 35rpx;
  347. font-weight: 700;
  348. }
  349. .text {
  350. font-size: 30rpx;
  351. color: #848484;
  352. padding-top: 50rpx;
  353. }
  354. .btn_box {
  355. margin: 70rpx 0 0 0;
  356. display: flex;
  357. align-items: center;
  358. justify-content: space-between;
  359. }
  360. }
  361. }
  362. .weixin {
  363. // background: linear-gradient(90deg, rgba(36, 214, 78, 1), rgba(45, 187, 89, 1));
  364. background: $base-color;
  365. width: 40%;
  366. color: #ffffff;
  367. border-radius: 50rpx;
  368. border: none;
  369. margin: 0 20rpx;
  370. }
  371. .bg-img {
  372. position: absolute;
  373. top: 0;
  374. left: 0;
  375. width: 100%;
  376. height: 100%;
  377. }
  378. .logo-img-box{
  379. width: 100%;
  380. height: 100vh;
  381. display: flex;
  382. flex-direction: column;
  383. align-items: center;
  384. // justify-content: center;
  385. }
  386. .logo-img {
  387. margin-top: 180rpx;
  388. background-color: pink;
  389. width: 300rpx;
  390. height: 300rpx;
  391. border-radius: 50%;
  392. }
  393. .userInfo {
  394. width: 80%;
  395. margin: 0 100rpx;
  396. margin-top: 80rpx;
  397. color: #ffffff;
  398. border-radius: 99rpx;
  399. // background-color: $base-color !important;
  400. background-color: #901b23 !important;
  401. }
  402. </style>