wxLogin.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <template>
  2. <view class="content">
  3. <!-- #ifdef MP -->
  4. <image class="bg-img" :src="/static/img/img09.png" mode=" scaleToFill"></image>
  5. <view class="logo-img-box">
  6. <image class="logo-img" src="/static/img/img10.png" mode=" aspectFit"></image>
  7. <button class="userInfo" type="warn" open-type="getUserInfo" @getuserinfo="userInfoData">
  8. <text class="iconfont iconweixin"></text>
  9. <text>微信授权登录</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 { wechatMpAuth } from '@/api/wx';
  31. // #endif
  32. import { mapMutations } from 'vuex';
  33. import { getUserInfo, bangding } from '@/api/login.js';
  34. export default {
  35. data() {
  36. return {
  37. userInfo: {}, //授权用户信息
  38. code: '', //授权code
  39. loding: false, //判断是否在点击中
  40. MaskShow: false // 手机号授权弹窗
  41. };
  42. },
  43. onLoad(option) {
  44. this.loadData();
  45. },
  46. methods: {
  47. loadData() {
  48. // #ifdef H5
  49. loginWinxin();
  50. // #endif
  51. },
  52. // #ifndef H5
  53. // 用户确认授权
  54. userInfoData(e) {
  55. const that = this;
  56. // #ifdef MP-WEIXIN
  57. if (!this.loding) {
  58. uni.showModal({
  59. title: '温馨提示',
  60. content: '亲,授权微信登录后才能正常使用小程序功能',
  61. success: res => {
  62. if (res.confirm) {
  63. wx.getUserProfile({
  64. desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  65. success: res => {
  66. that.userInfo = res;
  67. that.loadMp();
  68. },
  69. fail: err => {
  70. uni.showToast({
  71. title: '您拒绝了请求,不能正常使用小程序',
  72. icon: 'error',
  73. duration: 2000
  74. });
  75. return;
  76. }
  77. });
  78. } else if (res.cancel) {
  79. //如果用户点击了取消按钮
  80. uni.showToast({
  81. title: '您拒绝了请求,不能正常使用小程序',
  82. icon: 'error',
  83. duration: 2000
  84. });
  85. return;
  86. }
  87. }
  88. });
  89. }
  90. // #endif
  91. },
  92. // #endif
  93. // #ifdef MP-WEIXIN
  94. loadMp(option) {
  95. let obj = this;
  96. obj.loding = true;
  97. // 获取登录授权页数据
  98. let user = obj.userInfo;
  99. // 获取推广人id
  100. let spread_spid = uni.getStorageSync('spread') || '';
  101. // let spread_code = uni.getStorageSync('spread_code') || '';
  102. uni.showLoading({
  103. title: '授权中',
  104. mask: true
  105. });
  106. wx.login({
  107. success(e) {
  108. wechatMpAuth({
  109. code: e.code,
  110. iv: user.iv,
  111. encryptedData: user.encryptedData,
  112. spread_spid: spread_spid,
  113. // #ifdef MP
  114. spread_code: spread_code
  115. // #endif
  116. })
  117. .then(({ data }) => {
  118. obj.loding = false;
  119. obj.wchatAuth(data);
  120. console.log(data);
  121. })
  122. .catch(e => {
  123. obj.loding = false;
  124. uni.hideLoading();
  125. });
  126. },
  127. fill: function(e) {
  128. obj.loding = false;
  129. console.log(e);
  130. }
  131. });
  132. },
  133. wchatAuth(data) {
  134. let obj = this;
  135. // 保存token
  136. uni.setStorageSync('token', data.token);
  137. // 获取用户基础信息
  138. getUserInfo({})
  139. .then(e => {
  140. uni.hideLoading();
  141. obj.login();
  142. // 保存返回用户数据
  143. obj.setUserInfo(e.data);
  144. obj.ToIndex();
  145. // if (e.data.phone == null || !e.data.phone ) {
  146. // obj.MaskShow = true;
  147. // } else {
  148. // obj.ToIndex();
  149. // }
  150. })
  151. .catch(e => {
  152. uni.hideLoading();
  153. });
  154. },
  155. // #endif
  156. ToIndex() {
  157. let obj = this;
  158. let ur = uni.getStorageSync('present') || '/pages/index/index';
  159. // 用于处理缓存bug
  160. if (ur == 'pages/product/product') {
  161. ur = '/pages/index/index';
  162. }
  163. uni.switchTab({
  164. url: ur,
  165. fail(e) {
  166. uni.navigateTo({
  167. url: ur,
  168. fail(e) {
  169. uni.navigateTo({
  170. url: '/pages/index/index'
  171. });
  172. }
  173. });
  174. }
  175. });
  176. },
  177. // 绑定手机号
  178. PhoneNumber(e) {
  179. let obj = this;
  180. obj.MaskShow = false;
  181. (obj.iv = e.detail.iv), (obj.encryptedData = e.detail.encryptedData);
  182. uni.setStorageSync('code', obj.code);
  183. bangding({
  184. flag: 1,
  185. cache_key: obj.cache_key,
  186. code: obj.code,
  187. iv: obj.iv,
  188. encryptedData: obj.encryptedData
  189. }).then(function(e) {
  190. if (e.data.is_bind == 1) {
  191. console.log('bangding1');
  192. bangding({
  193. flag: 1,
  194. cache_key: obj.cache_key,
  195. code: obj.code,
  196. iv: obj.iv,
  197. encryptedData: obj.encryptedData,
  198. step: 1
  199. })
  200. .then(function(e) {
  201. // 获取用户基础信息
  202. obj.GetUser();
  203. obj.$api.msg(e.msg);
  204. obj.$nextTick(function() {
  205. obj.ToIndex();
  206. });
  207. })
  208. .catch(e => {
  209. console.log(e);
  210. });
  211. } else {
  212. console.log('bangding2');
  213. obj.$api.msg(e.msg);
  214. // 获取用户基础信息
  215. obj.GetUser();
  216. obj.$api.msg(e.msg);
  217. obj.$nextTick(function() {
  218. obj.ToIndex();
  219. });
  220. }
  221. });
  222. },
  223. GetUser() {
  224. // 获取用户基础信息
  225. getUserInfo({})
  226. .then(({ data }) => {
  227. this.setUserInfo(data);
  228. console.log(data, 11);
  229. console.log(uni.getStorageSync('userInfo'), 55);
  230. })
  231. .catch(e => {
  232. console.log(e);
  233. });
  234. }
  235. }
  236. };
  237. </script>
  238. <style lang="scss">
  239. page,
  240. .content {
  241. height: 100%;
  242. }
  243. .Mask {
  244. position: fixed;
  245. top: 0;
  246. width: 100%;
  247. height: 100%;
  248. background-color: rgba(51, 51, 51, 0.7);
  249. .Mask-box {
  250. margin: auto;
  251. margin-top: 320rpx;
  252. padding: 50rpx 30rpx;
  253. width: 90%;
  254. height: 450rpx;
  255. top: 500rpx;
  256. left: 10%;
  257. background-color: #ffffff;
  258. border-radius: 15rpx;
  259. .title {
  260. text-align: center;
  261. font-size: 35rpx;
  262. font-weight: 700;
  263. }
  264. .text {
  265. font-size: 30rpx;
  266. color: #848484;
  267. padding-top: 50rpx;
  268. }
  269. .btn_box {
  270. margin: 70rpx 0 0 0;
  271. display: flex;
  272. align-items: center;
  273. justify-content: space-between;
  274. }
  275. }
  276. }
  277. .weixin {
  278. // background: linear-gradient(90deg, rgba(36, 214, 78, 1), rgba(45, 187, 89, 1));
  279. background: $base-color;
  280. width: 40%;
  281. color: #ffffff;
  282. border-radius: 50rpx;
  283. border: none;
  284. margin: 0 20rpx;
  285. }
  286. .bg-img,
  287. .logo-img-box {
  288. position: absolute;
  289. top: 0;
  290. left: 0;
  291. width: 100%;
  292. height: 100%;
  293. }
  294. .logo-img {
  295. margin-top: 20vh;
  296. margin-left: 176rpx;
  297. width: 385rpx;
  298. height: 394rpx;
  299. }
  300. .userInfo {
  301. margin: 0 100rpx;
  302. margin-top: 50rpx;
  303. color: #ffffff;
  304. border-radius: 99rpx;
  305. background-color: $base-color !important;
  306. }
  307. </style>