wxLogin.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. <template>
  2. <view class="content">
  3. <!-- #ifdef MP -->
  4. <view class="flex-center">
  5. <view class="logo-img-box">
  6. <image class="logo-img" src="../../static/image/logo.png" mode="scaleToFill"></image>
  7. <button class="userInfo flex-center" type="warn" @tap="loding?'':userInfoData()">
  8. <image class="icon margin-r-10" src="../../static/icon/weichat.png" mode="widthFix"></image>
  9. <text>微信授权登录</text>
  10. </button>
  11. <agreement @checkedChange='changeChecked'></agreement>
  12. </view>
  13. </view>
  14. <view class="Mask" v-show="MaskShow">
  15. <view class="Mask-box">
  16. <view class="title">申请获取您的手机号</view>
  17. <view class="text">为了方便您的取货和我们的送货,并获取更多优惠活动,需要您的手机授权</view>
  18. <view class="btn_box">
  19. <button class="weixin" @click="outUser">取消</button>
  20. <button class="weixin" open-type="getPhoneNumber" @getphonenumber="PhoneNumber">授权</button>
  21. </view>
  22. </view>
  23. </view>
  24. <!-- #endif -->
  25. </view>
  26. </template>
  27. <script>
  28. // #ifdef H5
  29. import {
  30. loginWinxin
  31. } from '@/utils/wxAuthorized';
  32. // #endif
  33. // #ifdef MP-WEIXIN
  34. import {
  35. wechatMpAuth
  36. } from '@/api/wx';
  37. import {
  38. loginWinxinMp
  39. } from '@/utils/wxMinProgram';
  40. // #endif
  41. import {
  42. mapMutations
  43. } from 'vuex';
  44. import {
  45. getUserInfo,
  46. } from '@/api/login.js';
  47. import {
  48. binding
  49. } from '@/api/set.js';
  50. import agreement from "./agreement.vue"
  51. export default {
  52. components: {
  53. agreement
  54. },
  55. data() {
  56. return {
  57. checked: false,
  58. userInfo: {}, //授权用户信息
  59. code: '', //授权code
  60. loding: false, //判断是否在点击中
  61. MaskShow: false, // 手机号授权弹窗
  62. cache_key: '',
  63. successUrl:'/pages/home/index'
  64. };
  65. },
  66. onLoad(option) {
  67. console.log(option, '船只');
  68. this.loadData();
  69. },
  70. methods: {
  71. ...mapMutations('user', ['login', 'setUserInfo', 'logout']),
  72. changeChecked(check) {
  73. this.checked = check;
  74. },
  75. loadData() {
  76. let obj = this;
  77. // #ifdef H5
  78. loginWinxin();
  79. // #endif
  80. // #ifdef MP-WEIXIN
  81. loginWinxinMp().then((e) => {
  82. console.log(e, 'loginWinxinMp');
  83. obj.code = e.code;
  84. });
  85. // #endif
  86. },
  87. // #ifndef H5
  88. // 用户确认授权
  89. userInfoData(e) {
  90. const that = this;
  91. if (!that.checked) {
  92. uni.showModal({
  93. title: '提示',
  94. content: '请先阅读同意《母婴界严选服务》《隐私条例》',
  95. showCancel: false,
  96. });
  97. return;
  98. }
  99. // #ifdef MP-WEIXIN
  100. if (!this.loding) {
  101. wx.getUserProfile({
  102. desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  103. success: res => {
  104. console.log(res, 'res');
  105. that.userInfo = res;
  106. that.loadMp();
  107. },
  108. fail: err => {
  109. console.log(err, 'err');
  110. that.loding = false;
  111. that.getLoginCode();
  112. uni.showToast({
  113. title: '您拒绝了请求,不能正常使用小程序',
  114. icon: 'error',
  115. duration: 2000
  116. });
  117. }
  118. });
  119. }
  120. // #endif
  121. },
  122. // #endif
  123. // #ifdef MP-WEIXIN
  124. // 退出用户登录
  125. outUser() {
  126. this.logout();
  127. uni.switchTab({
  128. url: this.successUrl
  129. })
  130. },
  131. getLoginCode() {
  132. const that = this;
  133. wx.login({
  134. success(e) {
  135. console.log(e, 'loginWinxinMp');
  136. that.code = e.code;
  137. },
  138. fill: function(e) {
  139. console.log(e)
  140. }
  141. })
  142. },
  143. // 小程序授权登录
  144. loadMp(option) {
  145. let obj = this;
  146. obj.loding = true;
  147. // 获取登录授权页数据
  148. let user = obj.userInfo;
  149. // 获取推广人id
  150. // #ifndef MP
  151. let spread_spid = uni.getStorageSync('spread') || '';
  152. // #endif
  153. // #ifdef MP
  154. let spread_code = uni.getStorageSync('spread_code') || '';
  155. // #endif
  156. uni.showLoading({
  157. title: '授权中',
  158. mask: true
  159. });
  160. wechatMpAuth({
  161. code: obj.code,
  162. avatar: user.userInfo.avatarUrl,
  163. nickname: user.userInfo.nickName,
  164. gender: user.userInfo.gender,
  165. // #ifndef MP
  166. parent_uid: spread_spid,
  167. // #endif
  168. // #ifdef MP
  169. parent_uid: spread_code
  170. // #endif
  171. })
  172. .then(({
  173. data
  174. }) => {
  175. obj.loding = false;
  176. obj.wchatAuth(data);
  177. console.log(data, '授权数据');
  178. })
  179. .catch(e => {
  180. obj.loding = false;
  181. uni.hideLoading();
  182. });
  183. },
  184. wchatAuth(data) {
  185. let obj = this;
  186. obj.cache_key = data.cache_key;
  187. // 保存token
  188. uni.setStorageSync('token', data.token);
  189. obj.GetUser().then((data) => {
  190. console.log(data, '返回用户的数据');
  191. try {
  192. uni.hideLoading();
  193. obj.login();
  194. // 保存返回用户数据
  195. obj.setUserInfo(data);
  196. if (data.mobile == null || !data.mobile) {
  197. obj.MaskShow = true;
  198. } else {
  199. obj.ToIndex();
  200. }
  201. } catch (e) {
  202. console.log(e, '登录');
  203. }
  204. }).catch((e) => {
  205. uni.hideLoading();
  206. })
  207. },
  208. // #endif
  209. ToIndex() {
  210. const that = this;
  211. let ur = uni.getStorageSync('present') ||that.successUrl;
  212. // 用于处理缓存bug
  213. if (ur == 'pages/product/product') {
  214. ur = that.successUrl;
  215. }
  216. uni.switchTab({
  217. url: ur,
  218. fail(e) {
  219. uni.navigateTo({
  220. url: ur,
  221. fail(e) {
  222. uni.navigateTo({
  223. url: that.successUrl
  224. });
  225. }
  226. });
  227. }
  228. });
  229. },
  230. // 获取用户数据
  231. GetUser() {
  232. // 获取用户基础信息
  233. return new Promise((ok, error) => {
  234. getUserInfo({})
  235. .then(({
  236. data
  237. }) => {
  238. ok(data)
  239. })
  240. .catch(e => {
  241. error(e)
  242. });
  243. })
  244. },
  245. // #ifdef MP
  246. // 绑定手机号
  247. PhoneNumber(e) {
  248. let obj = this;
  249. obj.MaskShow = false;
  250. let requestData = {
  251. code: e.detail.code,
  252. }
  253. obj.bindPhone(requestData).then((ar) => {
  254. obj.GetUser().then((data) => {
  255. obj.setUserInfo(data);
  256. uni.showToast({
  257. title: '绑定成功',
  258. mask: true,
  259. });
  260. setTimeout(
  261. function() {
  262. obj.ToIndex();
  263. }, 500
  264. )
  265. });
  266. })
  267. },
  268. // 绑定手机号
  269. bindPhone(data) {
  270. // 获取用户基础信息
  271. return new Promise((ok, error) => {
  272. binding(data)
  273. .then(function(e) {
  274. ok(e)
  275. })
  276. .catch(e => {
  277. error(e)
  278. });
  279. })
  280. }
  281. // #endif
  282. }
  283. };
  284. </script>
  285. <style lang="scss">
  286. page,
  287. .content {
  288. height: 100%;
  289. /* #ifdef MP-WEIXIN */
  290. .flex-center {
  291. height: 100%;
  292. text-align: center;
  293. line-height: 1;
  294. }
  295. /* #endif */
  296. }
  297. /* #ifdef MP-WEIXIN */
  298. .Mask {
  299. position: fixed;
  300. top: 0;
  301. width: 100%;
  302. height: 100%;
  303. background-color: rgba(51, 51, 51, 0.7);
  304. .Mask-box {
  305. margin: auto;
  306. margin-top: 320rpx;
  307. padding: 50rpx 30rpx;
  308. width: 90%;
  309. height: 450rpx;
  310. top: 500rpx;
  311. left: 10%;
  312. background-color: #ffffff;
  313. border-radius: 15rpx;
  314. .title {
  315. text-align: center;
  316. font-size: 35rpx;
  317. font-weight: 700;
  318. }
  319. .text {
  320. font-size: 30rpx;
  321. color: #848484;
  322. padding-top: 50rpx;
  323. }
  324. .btn_box {
  325. margin: 70rpx 0 0 0;
  326. display: flex;
  327. align-items: center;
  328. justify-content: space-between;
  329. }
  330. }
  331. }
  332. .weixin {
  333. background: $base-color;
  334. width: 40%;
  335. color: #ffffff;
  336. border-radius: 50rpx;
  337. border: none;
  338. margin: 0 20rpx;
  339. }
  340. .logo-img {
  341. width: 385rpx;
  342. height: 385rpx;
  343. margin: 0 auto;
  344. border-radius: 50rpx;
  345. }
  346. .userInfo {
  347. margin: 0 100rpx;
  348. margin-top: 50rpx;
  349. padding: 30rpx 50rpx;
  350. color: #ffffff;
  351. border-radius: 99rpx;
  352. background-color: $base-color !important;
  353. .icon {
  354. width: 55rpx;
  355. }
  356. }
  357. /* #endif */
  358. </style>