Login.vue 8.0 KB

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