wxLogin.vue 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. });
  181. },
  182. wchatAuth(data) {
  183. let obj = this;
  184. obj.cache_key = data.cache_key;
  185. // 保存token
  186. uni.setStorageSync('token', data.token);
  187. obj.GetUser().then((data) => {
  188. console.log(data, '返回用户的数据');
  189. try {
  190. uni.hideLoading();
  191. obj.login();
  192. // 保存返回用户数据
  193. obj.setUserInfo(data);
  194. console.log();
  195. if (data.phone == null || !data.phone) {
  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. console.log(e, '授权返回数据');
  251. obj.MaskShow = false;
  252. (obj.iv = e.detail.iv), (obj.encryptedData = e.detail.encryptedData);
  253. uni.setStorageSync('code', obj.code);
  254. let requestData = {
  255. flag: 1,
  256. cache_key: obj.cache_key,
  257. code: obj.code,
  258. iv: obj.iv,
  259. encryptedData: obj.encryptedData
  260. }
  261. obj.bindPhone(requestData).then((ar) => {
  262. if (ar.data.is_bind == 1) {
  263. uni.showToast({
  264. title:e.msg,
  265. icon:"none"
  266. })
  267. } else {
  268. obj.GetUser().then((data) => {
  269. obj.setUserInfo(data);
  270. obj.ToIndex();
  271. });
  272. }
  273. })
  274. },
  275. // 绑定手机号
  276. bindPhone(data) {
  277. // 获取用户基础信息
  278. return new Promise((ok, error) => {
  279. binding(data)
  280. .then(function(e) {
  281. ok(e)
  282. })
  283. .catch(e => {
  284. error(e)
  285. });
  286. })
  287. }
  288. // #endif
  289. }
  290. };
  291. </script>
  292. <style lang="scss">
  293. page,
  294. .content {
  295. height: 100%;
  296. /* #ifdef MP-WEIXIN */
  297. .flex-center {
  298. height: 100%;
  299. text-align: center;
  300. line-height: 1;
  301. }
  302. /* #endif */
  303. }
  304. /* #ifdef MP-WEIXIN */
  305. .Mask {
  306. position: fixed;
  307. top: 0;
  308. width: 100%;
  309. height: 100%;
  310. background-color: rgba(51, 51, 51, 0.7);
  311. .Mask-box {
  312. margin: auto;
  313. margin-top: 320rpx;
  314. padding: 50rpx 30rpx;
  315. width: 90%;
  316. height: 450rpx;
  317. top: 500rpx;
  318. left: 10%;
  319. background-color: #ffffff;
  320. border-radius: 15rpx;
  321. .title {
  322. text-align: center;
  323. font-size: 35rpx;
  324. font-weight: 700;
  325. }
  326. .text {
  327. font-size: 30rpx;
  328. color: #848484;
  329. padding-top: 50rpx;
  330. }
  331. .btn_box {
  332. margin: 70rpx 0 0 0;
  333. display: flex;
  334. align-items: center;
  335. justify-content: space-between;
  336. }
  337. }
  338. }
  339. .weixin {
  340. background: $base-color;
  341. width: 40%;
  342. color: #ffffff;
  343. border-radius: 50rpx;
  344. border: none;
  345. margin: 0 20rpx;
  346. }
  347. .logo-img {
  348. width: 385rpx;
  349. height: 385rpx;
  350. margin: 0 auto;
  351. border-radius: 50rpx;
  352. }
  353. .userInfo {
  354. margin: 0 100rpx;
  355. margin-top: 50rpx;
  356. padding: 30rpx 50rpx;
  357. color: #ffffff;
  358. border-radius: 99rpx;
  359. background-color: $base-color !important;
  360. .icon{
  361. width: 55rpx;
  362. height: 55rpx;
  363. }
  364. }
  365. /* #endif */
  366. </style>