wxLogin.vue 6.8 KB

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