wxLogin.vue 7.6 KB

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