wxLogin.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 { loginWinxinMp } from '@/utils/wxMinProgram';
  31. import { wechatMpAuth } from '@/api/wx';
  32. // #endif
  33. import { mapMutations } from 'vuex';
  34. import { getUserInfo, bangding } from '@/api/login.js';
  35. export default {
  36. data() {
  37. return {
  38. userInfo: {}, //授权用户信息
  39. code: '', //授权code
  40. loding: false, //判断是否在点击中
  41. MaskShow: false // 手机号授权弹窗
  42. };
  43. },
  44. onLoad(option) {
  45. this.loadData();
  46. },
  47. methods: {
  48. loadData() {
  49. // #ifdef H5
  50. loginWinxin();
  51. // #endif
  52. },
  53. // #ifndef H5
  54. // 用户确认授权
  55. userInfoData(e) {
  56. this.userInfo = e;
  57. // #ifdef MP-WEIXIN
  58. if (!this.loding) {
  59. this.loadMp();
  60. }
  61. // #endif
  62. },
  63. // #endif
  64. // #ifdef MP-WEIXIN
  65. loadMp(option) {
  66. let obj = this;
  67. obj.loding = true;
  68. // 获取登录授权页数据
  69. let user = obj.userInfo;
  70. // 获取推广人id
  71. let spread_spid = uni.getStorageSync('spread') || '';
  72. uni.showLoading({
  73. title: '授权中',
  74. mask:true
  75. });
  76. loginWinxinMp().then(e => {
  77. this.code = e.code;
  78. wechatMpAuth({
  79. code: this.code,
  80. iv: user.target.iv,
  81. encryptedData: user.target.encryptedData,
  82. spread_spid: spread_spid
  83. })
  84. .then(({ data }) => {
  85. obj.loding = false;
  86. obj.wchatAuth(data);
  87. })
  88. .catch(e => {
  89. obj.loding = false;
  90. uni.hideLoading();
  91. });
  92. }).catch((e) => {
  93. uni.hideLoading();
  94. });
  95. },
  96. wchatAuth(data) {
  97. let obj = this;
  98. // 保存token
  99. uni.setStorageSync('token', data.token);
  100. // 获取用户基础信息
  101. getUserInfo({})
  102. .then(e => {
  103. uni.hideLoading();
  104. obj.login();
  105. // 保存返回用户数据
  106. obj.setUserInfo(e.data);
  107. obj.ToIndex();
  108. // if (e.data.phone == null || !e.data.phone ) {
  109. // obj.MaskShow = true;
  110. // } else {
  111. // obj.ToIndex();
  112. // }
  113. })
  114. .catch(e => {
  115. uni.hideLoading();
  116. });
  117. },
  118. // #endif
  119. ToIndex() {
  120. let obj = this;
  121. let ur = uni.getStorageSync('present') || '/pages/index/index';
  122. // 用于处理缓存bug
  123. if (ur == 'pages/product/product') {
  124. ur = '/pages/index/index';
  125. }
  126. uni.switchTab({
  127. url: ur,
  128. fail(e) {
  129. uni.navigateTo({
  130. url: ur,
  131. fail(e) {
  132. uni.navigateTo({
  133. url: '/pages/index/index'
  134. });
  135. }
  136. });
  137. }
  138. });
  139. },
  140. // 绑定手机号
  141. PhoneNumber(e) {
  142. let obj = this;
  143. obj.MaskShow = false;
  144. (obj.iv = e.detail.iv), (obj.encryptedData = e.detail.encryptedData);
  145. uni.setStorageSync('code', obj.code);
  146. bangding({
  147. flag: 1,
  148. cache_key: obj.cache_key,
  149. code: obj.code,
  150. iv: obj.iv,
  151. encryptedData: obj.encryptedData
  152. }).then(function(e) {
  153. if (e.data.is_bind == 1) {
  154. console.log('bangding1');
  155. bangding({
  156. flag: 1,
  157. cache_key: obj.cache_key,
  158. code: obj.code,
  159. iv: obj.iv,
  160. encryptedData: obj.encryptedData,
  161. step: 1
  162. })
  163. .then(function(e) {
  164. // 获取用户基础信息
  165. obj.GetUser();
  166. obj.$api.msg(e.msg);
  167. obj.$nextTick(function() {
  168. obj.ToIndex();
  169. });
  170. })
  171. .catch(e => {
  172. console.log(e);
  173. });
  174. } else {
  175. console.log('bangding2');
  176. obj.$api.msg(e.msg);
  177. // 获取用户基础信息
  178. obj.GetUser();
  179. obj.$api.msg(e.msg);
  180. obj.$nextTick(function() {
  181. obj.ToIndex();
  182. });
  183. }
  184. });
  185. },
  186. GetUser() {
  187. // 获取用户基础信息
  188. getUserInfo({})
  189. .then(({ data }) => {
  190. this.setUserInfo(data);
  191. console.log(data, 11);
  192. console.log(uni.getStorageSync('userInfo'), 55);
  193. })
  194. .catch(e => {
  195. console.log(e);
  196. });
  197. }
  198. }
  199. };
  200. </script>
  201. <style lang="scss">
  202. page,
  203. .content {
  204. height: 100%;
  205. }
  206. .Mask {
  207. position: fixed;
  208. top: 0;
  209. width: 100%;
  210. height: 100%;
  211. background-color: rgba(51, 51, 51, 0.7);
  212. .Mask-box {
  213. margin: auto;
  214. margin-top: 320rpx;
  215. padding: 50rpx 30rpx;
  216. width: 90%;
  217. height: 450rpx;
  218. top: 500rpx;
  219. left: 10%;
  220. background-color: #ffffff;
  221. border-radius: 15rpx;
  222. .title {
  223. text-align: center;
  224. font-size: 35rpx;
  225. font-weight: 700;
  226. }
  227. .text {
  228. font-size: 30rpx;
  229. color: #848484;
  230. padding-top: 50rpx;
  231. }
  232. .btn_box {
  233. margin: 70rpx 0 0 0;
  234. display: flex;
  235. align-items: center;
  236. justify-content: space-between;
  237. }
  238. }
  239. }
  240. .weixin {
  241. // background: linear-gradient(90deg, rgba(36, 214, 78, 1), rgba(45, 187, 89, 1));
  242. background: $base-color;
  243. width: 40%;
  244. color: #ffffff;
  245. border-radius: 50rpx;
  246. border: none;
  247. margin: 0 20rpx;
  248. }
  249. .bg-img,
  250. .logo-img-box {
  251. position: absolute;
  252. top: 0;
  253. left: 0;
  254. width: 100%;
  255. height: 100%;
  256. }
  257. .logo-img {
  258. margin-top: 20vh;
  259. margin-left: 176rpx;
  260. width: 385rpx;
  261. height: 394rpx;
  262. }
  263. .userInfo {
  264. margin: 0 100rpx;
  265. margin-top: 50rpx;
  266. color: #ffffff;
  267. border-radius: 99rpx;
  268. background-color: $base-color !important;
  269. }
  270. </style>