wxLogin.vue 6.4 KB

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