wxLogin.vue 8.2 KB

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