Login.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <template>
  2. <view class="login-btn-box">
  3. <!-- #ifdef MP-WEIXIN -->
  4. <button v-if="canIUseGetUserProfile" @click="getUserProfile" class="login-btn primary-color"><slot name="lBtn">登录/注册</slot></button>
  5. <button v-else class="login-btn primary-color" open-type="getUserInfo" @getuserinfo="getuserinfo"><slot name="lBtn">登录/注册</slot></button>
  6. <!-- #endif -->
  7. <!-- #ifdef MP-TOUTIAO -->
  8. <button class="login-btn primary-color" @click="getuserinfoTT"><slot name="lBtn">登录/注册</slot></button>
  9. <!-- #endif -->
  10. <!-- #ifdef APP-PLUS || H5 -->
  11. <button class="login-btn primary-color" @click="appLogin"><slot name="lBtn">去登录</slot></button>
  12. <!-- #endif -->
  13. </view>
  14. </template>
  15. <script>
  16. export default {
  17. data() {
  18. return {
  19. userInfo: {},
  20. wxUserInfo: {},
  21. loginCode: '',
  22. provider: '',
  23. canIUseGetUserProfile: false
  24. };
  25. },
  26. created() {
  27. // #ifdef MP-WEIXIN
  28. if (!this.$common.isLogin()) {
  29. uni.login({
  30. provider: 'weixin',
  31. success: res => {
  32. this.loginCode = res.code;
  33. }
  34. });
  35. }
  36. this.canIUseGetUserProfile = !!uni.getUserProfile;
  37. // #endif
  38. },
  39. methods: {
  40. // APP登录
  41. appLogin() {
  42. // #ifdef APP-PLUS
  43. this.$emit('loginSuccess');
  44. const res = uni.getSystemInfoSync();
  45. if (res.platform === 'ios') {
  46. const iosAuditStatus = this.$store.state.iosAuditStatus;
  47. const isUpdate = uni.getStorageSync('isUpdate');
  48. if (iosAuditStatus === 4 && !isUpdate) {
  49. this.goPage('/pagesT/public/login');
  50. } else {
  51. this.goPage('/pagesT/public/wxLogin');
  52. }
  53. } else {
  54. this.goPage('/pagesT/public/wxLogin');
  55. }
  56. // #endif
  57. // #ifdef H5
  58. this.$emit('loginSuccess');
  59. this.goPage('/pagesT/public/wxLogin');
  60. // #endif
  61. },
  62. // 拒绝跳转主页面
  63. goIndex() {
  64. uni.switchTab({
  65. url: '/pages/index/index'
  66. });
  67. },
  68. // 头条
  69. getuserinfoTT() {
  70. uni.login({
  71. provider: 'toutiao',
  72. success: res => {
  73. uni.setStorageSync('loginCode', res.code);
  74. this.loginCode = res.code;
  75. uni.getUserInfo({
  76. provider: 'toutiao',
  77. success: prop => {
  78. console.log(prop);
  79. this.wxUserInfo = prop;
  80. // 用户图像
  81. const avator = prop.userInfo.avatarUrl;
  82. uni.setStorageSync('avator', avator);
  83. // 用户昵称
  84. const NickName = prop.userInfo.nickName;
  85. uni.setStorageSync('NickName', NickName);
  86. this.microappLogin(this.loginCode);
  87. }
  88. });
  89. }
  90. });
  91. },
  92. getUserProfile() {
  93. wx.getUserProfile({
  94. desc: '用于完善用户资料',
  95. lang: 'zh_CN',
  96. success: res => {
  97. console.log(res);
  98. console.log('获取用户授权信息成功');
  99. this.getuserinfo(res);
  100. },
  101. fail: fail => {
  102. console.log('获取用户授权信息失败');
  103. }
  104. });
  105. },
  106. // 允许获取信息 微信
  107. getuserinfo(val) {
  108. this.$emit('loginSuccess');
  109. this.wxUserInfo = val.detail || val;
  110. // 用户图像
  111. const avator = this.wxUserInfo.userInfo.avatarUrl;
  112. uni.setStorageSync('avator', avator);
  113. // 用户昵称
  114. const NickName = this.wxUserInfo.userInfo.nickName;
  115. uni.setStorageSync('NickName', NickName);
  116. if (!this.loginCode) {
  117. uni.login({
  118. provider: 'weixin',
  119. success: res => {
  120. this.loginCode = res.code;
  121. this.Login(this.loginCode);
  122. }
  123. });
  124. } else {
  125. this.Login(this.loginCode);
  126. }
  127. },
  128. // 头条小程序登陆
  129. microappLogin(code) {
  130. this.$u.api
  131. .microappLogin({
  132. code: code
  133. })
  134. .then(({ data }) => {
  135. uni.setStorageSync('openid', data.openid);
  136. this.checkBindMiniProgram(data.openid, '');
  137. });
  138. },
  139. // 微信小程序通过code获取openId
  140. Login(code) {
  141. uni.showLoading();
  142. this.$u.api
  143. .getOpenId({
  144. code: code
  145. })
  146. .then(data => {
  147. uni.hideLoading();
  148. // code 用过之后 清除缓存的code
  149. this.loginCode = '';
  150. if (!data.data.unionid) {
  151. this.decryptData(this.wxUserInfo.iv, this.wxUserInfo.encryptedData, data.data.openid);
  152. } else {
  153. uni.setStorageSync('openid', data.data.openid);
  154. uni.setStorageSync('unionid', data.data.unionid);
  155. this.checkBindMiniProgram(data.data.openid, data.data.unionid);
  156. }
  157. // this.checkBindMiniProgram(data.data.openid, data.data.unionid);
  158. // uni.setStorageSync('session_key', data.data.session_key);
  159. });
  160. },
  161. // 反解 unionId
  162. decryptData(iv, encryptedData, openid) {
  163. // const iv = e.detail.iv;
  164. // const encryptedData = e.detail.encryptedData;
  165. this.$u.api
  166. .decryptData({
  167. openid: openid,
  168. iv: iv,
  169. encryptedData: encryptedData
  170. })
  171. .then(data => {
  172. uni.setStorageSync('openid', openid);
  173. uni.setStorageSync('unionid', data.data.unionId);
  174. this.checkBindMiniProgram(openid, data.data.unionId);
  175. });
  176. },
  177. // 判断是否已绑定 checkBindMiniProgram
  178. checkBindMiniProgram(openid, unionid) {
  179. this.$u.api
  180. .checkBindMiniProgram({
  181. openId: openid || '',
  182. unionId: unionid || '',
  183. source: this.$common.source()
  184. })
  185. .then(async data => {
  186. // 如果返回空绑定手机
  187. if (!data.data.token) {
  188. setTimeout(() => {
  189. this.goPage(`/pagesT/binding/bindPhone`);
  190. }, 100);
  191. return;
  192. } else {
  193. const token = data.data.token;
  194. uni.setStorageSync('token', token);
  195. uni.setStorageSync('expireTime', data.data.expireTime);
  196. // 更新登录状态
  197. this.$store.commit('commit_hasLogin', true);
  198. // 添加分销上下级关系
  199. await this.relationshipBusinessman();
  200. // 否则验证审核
  201. await this.getCustomerInfo(data.data.token, data.data.expireTime);
  202. }
  203. });
  204. },
  205. // 添加分销上下级关系 source: 1:首次点击链接 2:首次下单 3:首次付款'
  206. async relationshipBusinessman() {
  207. if (!this.$store.state.businessmanId) {
  208. return;
  209. }
  210. this.$u.api
  211. .relationshipBusinessman({
  212. businessmanId: this.$store.state.businessmanId,
  213. source: 1
  214. })
  215. .then(res => {
  216. console.log('上下:', res);
  217. });
  218. },
  219. //先获取个人资料
  220. getCustomerInfo(token) {
  221. this.$u.api.getCustomerInfo().then(({ data }) => {
  222. if (data.enableStatus === 4) {
  223. uni.clearStorageSync();
  224. uni.showModal({
  225. title: '账号已被禁用,您可以',
  226. content: '联系电话:' + (data.enterpriseMobile||''),
  227. confirmText: '联系客服',
  228. cancelText: '取消',
  229. success: res => {
  230. if (res.confirm) {
  231. uni.makePhoneCall({
  232. phoneNumber: data.enterpriseMobile
  233. });
  234. } else {
  235. this.goPage('/pages/binding/bindInfo', 'reLaunch');
  236. }
  237. }
  238. });
  239. return;
  240. }
  241. // if (this.$store.state.baseSet.personnelReview === 5) {
  242. if (data.status === 3) {
  243. // uni.clearStorageSync();
  244. // 说明资料不完善,跳转到完善资料的页面
  245. uni.showModal({
  246. title: '审核未通过,请重新修改资料',
  247. content: data.auditFailReason || '',
  248. cancelText: '联系客服',
  249. confirmText: '修改资料',
  250. success: res => {
  251. if (res.confirm) {
  252. this.goPage('/pagesT/user/editUserInfo?token=' + token);
  253. } else if (res.cancel) {
  254. uni.makePhoneCall({
  255. phoneNumber: data.enterpriseMobile
  256. });
  257. }
  258. }
  259. });
  260. } else if (data.status === 1) {
  261. // uni.clearStorageSync();
  262. uni.showModal({
  263. title: '账号正在审核中,您可以',
  264. content: '联系电话:' + (data.enterpriseMobile||''),
  265. confirmText: '先去逛逛',
  266. cancelText: '联系客服',
  267. success: res => {
  268. if (res.confirm) {
  269. this.goPage('/pages/index/index', 'switchTab');
  270. } else {
  271. uni.makePhoneCall({
  272. phoneNumber: data.enterpriseMobile
  273. });
  274. }
  275. }
  276. });
  277. } else {
  278. this.$emit('loginFinish');
  279. }
  280. });
  281. }
  282. }
  283. };
  284. </script>
  285. <style lang="scss">
  286. .login-btn {
  287. font-size: 28rpx;
  288. // height: 40upx;
  289. line-height: inherit;
  290. background: transparent;
  291. color: $uni-color-primary;
  292. // width: 200upx;
  293. margin-left: 0;
  294. margin-right: 0;
  295. // display: inline-block;
  296. }
  297. .login-btn::after {
  298. border: 0 none;
  299. }
  300. </style>