login.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. <template>
  2. <view class="container">
  3. <global-loading />
  4. <view style="padding: 68upx 0 0 56upx;color: #ffffff;">
  5. <view style="font-weight: bold;font-style: oblique;">
  6. <view style="font-size: 58upx;">欢迎来到</view>
  7. <view style="font-size: 62upx;">巨蜂本地生活</view>
  8. </view>
  9. <view style="margin-top: 12upx;;font-size: 30upx;">
  10. <text>数字化营销的领跑者</text>
  11. </view>
  12. </view>
  13. <view style="display: flex;flex-direction: column;justify-content: center;align-items: center;margin-top: 80upx;">
  14. <view
  15. v-if="(wxLoginQuery.terminal === 3) || (wxLoginQuery.terminal === 2)" style="text-align: center;"
  16. @click="handleWXLogin"
  17. >
  18. <image style="width: 118upx;height: 98upx;" src="../../static/images/icon/we-chat.png" mode="" />
  19. <tui-button type="white" width="480rpx" height="82rpx" margin="40upx 0 0" shape="circle" @click="handleWXLogin">
  20. 微信一键登录
  21. </tui-button>
  22. </view>
  23. <!-- #ifdef MP-ALIPAY -->
  24. <tui-button
  25. type="danger" width="480rpx" height="82rpx" margin="70upx 0 0"
  26. shape="circle"
  27. @click="GetAliPayUserInfo"
  28. >
  29. 支付宝登录
  30. </tui-button>
  31. <!-- #endif -->
  32. <tui-button
  33. type="white" width="480rpx" height="82rpx" margin="70upx 0 0"
  34. shape="circle"
  35. @click="$switchTab('/pages/tabbar/index/index')"
  36. >
  37. 去首页逛逛
  38. </tui-button>
  39. </view>
  40. <view class="otherLoginButton">
  41. <view class="orderButtonItem">
  42. <view style="display: flex;flex-direction: column;align-items: center;">
  43. <view style="width: fit-content;padding: 12upx;background-color: #ffffff;border-radius: 48upx;">
  44. <tui-icon
  45. name="wealth-fill" color="#ce2601" :size="24"
  46. @click="$jump('/pages_category_page2/userModule/register', '', 0, 'redirect')"
  47. ></tui-icon>
  48. </view>
  49. <view style="margin-top: 12upx;font-size: 26upx;color: #ffffff;">注册</view>
  50. </view>
  51. <view style="display: flex;flex-direction: column;align-items: center;">
  52. <view style="width: fit-content;padding: 12upx;background-color: #ffffff;border-radius: 48upx;">
  53. <tui-icon
  54. name="mail-fill" color="#ce2601" :size="24"
  55. @click="$jump('/pages_category_page2/userModule/accountLogin?type=verificationCode')"
  56. ></tui-icon>
  57. </view>
  58. <view style="margin-top: 12upx;font-size: 26upx;color: #ffffff;">短信登录</view>
  59. </view>
  60. <view style="display: flex;flex-direction: column;align-items: center;">
  61. <view style="width: fit-content;padding: 12upx;background-color: #ffffff;border-radius: 48upx;">
  62. <tui-icon
  63. name="pwd" color="#ce2601" :size="24"
  64. @click="$jump('/pages_category_page2/userModule/accountLogin?type=password')"
  65. ></tui-icon>
  66. </view>
  67. <view style="margin-top: 12upx;font-size: 26upx;color: #ffffff;">密码登录</view>
  68. </view>
  69. </view>
  70. </view>
  71. <view class="agreement">
  72. 登录即同意<text class="colorText">《联通统一认证服务条款》</text>和<text class="colorText">《巨蜂商城用户 服务协议》</text>、<text
  73. class="colorText"
  74. >
  75. 《隐私政策》
  76. </text>并使用本机号码登陆
  77. </view>
  78. </view>
  79. </template>
  80. <script>
  81. import { J_STORAGE_KEY } from '../../config/constant'
  82. const NET = require('../../utils/request')
  83. const API = require('../../config/api')
  84. import { updateWXLoginApi } from '../../api/user'
  85. import { isInWx, getUrlCode } from '../../utils'
  86. export default {
  87. name: 'Login',
  88. data() {
  89. return {
  90. wxLoginQuery: {
  91. code: '',
  92. salesId: 0,
  93. headImage: '',
  94. terminal: 0
  95. }
  96. }
  97. },
  98. onLoad(options) {
  99. if (isInWx()) {
  100. this.wxLoginQuery.terminal = 3
  101. } else {
  102. // #ifdef H5
  103. this.wxLoginQuery.terminal = 5 // H5包含pc和移动端浏览器和微信浏览器的可能
  104. // #endif
  105. // #ifdef APP
  106. this.wxLoginQuery.terminal = 1
  107. // #endif
  108. // #ifdef MP-WEIXIN
  109. this.wxLoginQuery.terminal = 2
  110. // #endif
  111. // #ifdef MP-ALIPAY
  112. this.wxLoginQuery.terminal = 4
  113. // #endif
  114. }
  115. const pages = getCurrentPages()
  116. uni.setStorageSync('last_page', pages[pages.length - 2].route)
  117. if (pages[pages.length - 2].options) {
  118. uni.setStorageSync('last_page_options', pages[pages.length - 2].options)
  119. }
  120. },
  121. onShow() {
  122. // 所有需要授权页面进登录页面取消loading
  123. uni.hideLoading()
  124. if (isInWx()) {
  125. const code = getUrlCode().code
  126. if (code) this.handleWXLogin()
  127. }
  128. },
  129. onBackPress() {
  130. this.$switchTab('/pages/tabbar/index/index')
  131. return true
  132. },
  133. methods: {
  134. // 微信登录
  135. async handleWXLogin() {
  136. // uni.showLoading({
  137. // title: '正在加载...',
  138. // mask: true
  139. // });
  140. // // #ifdef APP
  141. // uni.login({
  142. // provider: 'weixin',
  143. // success: function(loginRes) {
  144. // // 获取用户信息
  145. // uni.getUserInfo({
  146. // provider: 'weixin',
  147. // success: function(infoRes) {
  148. // let wechatOpenId = infoRes.userInfo.openId
  149. // let avatarUrl = infoRes.userInfo.avatarUrl
  150. // updateWXAppLoginApi({
  151. // 'wechatOpenId': wechatOpenId
  152. // }).then(res => {
  153. // let data = {
  154. // wechatOpenId: wechatOpenId,
  155. // headImage: avatarUrl
  156. // }
  157. // that.handleLoginSuccess(res.data, data)
  158. // }).catch(res => {
  159. // console.log('failed: ', res)
  160. // })
  161. // },
  162. // fail: (err) => {
  163. // this.$showToast('微信登录授权失败')
  164. // }
  165. // });
  166. // }
  167. // });
  168. // // #endif
  169. if (this.wxLoginQuery.terminal === 2) {
  170. uni.login({
  171. provider: 'weixin',
  172. success: (res2) => {
  173. uni.hideLoading()
  174. this.wxLoginQuery.code = res2.code
  175. },
  176. fail: (err) => {
  177. this.$showToast('微信登录授权失败')
  178. }
  179. })
  180. uni.getUserProfile({
  181. desc: '正在获取', // 不写不弹提示框
  182. success: (res) => {
  183. this.wxLoginQuery.headImage = res.userInfo.avatarUrl
  184. // this.handleLoginSuccess({'ifFirst': 1}, {'headImg': res.userInfo.avatarUrl})
  185. updateWXLoginApi(this.wxLoginQuery)
  186. .then((data) => {
  187. this.handleLoginSuccess(data, { wechatOpenId: data.wechatOpenId, headImage: data.headImage })
  188. })
  189. },
  190. fail: (err) => {
  191. this.$showToast('微信登录授权失败')
  192. }
  193. })
  194. } else if (this.wxLoginQuery.terminal === 3) {
  195. const appid = 'wx603b04a561e4683e' // wxdf390bb4f8a67641
  196. const local = 'https://nsh5.tuanfengkeji.cn/#/pages_category_page2/userModule/login'
  197. const code = getUrlCode().code
  198. // console.log('获取code', code)
  199. if (code === null || code === undefined || code === '') {
  200. window.location.href =
  201. 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' +
  202. appid +
  203. '&redirect_uri=' +
  204. encodeURIComponent(local) +
  205. '&response_type=code&scope=snsapi_userinfo#wechat_redirect'
  206. } else {
  207. this.wxLoginQuery.code = code
  208. const { data } = await updateWXLoginApi(this.wxLoginQuery)
  209. this.handleLoginSuccess(data, { wechatOpenId: data.wechatOpenId, headImage: data.headImage }, 'WX')
  210. }
  211. }
  212. },
  213. GetAliPayUserInfo() {
  214. const that = this
  215. uni.login({
  216. provider: 'alipay',
  217. scopes: 'auth_user',
  218. success(loginRes) {
  219. that.alipayLogin(loginRes.authCode)
  220. }
  221. })
  222. },
  223. alipayLogin(authCode) {
  224. NET.request(API.AlipayLogin, {
  225. 'code': authCode
  226. }, 'POST').then((res) => {
  227. this.handleLoginSuccess(res.data, {
  228. 'buyerUserId': res.data.buyerUserId
  229. })
  230. })
  231. },
  232. handleLoginSuccess(buyerUser, data, type = '') {
  233. if (type === 'WX') {
  234. if (buyerUser.ifFirst == 0) {
  235. uni.setStorageSync(J_STORAGE_KEY, buyerUser)
  236. this.bindSalesCustomer()
  237. window.location.replace('https://nsh5.tuanfengkeji.cn/#/pages/tabbar/user/index')
  238. } else {
  239. window.location.replace(`https://nsh5.tuanfengkeji.cn/#/pages_category_page2/userModule/bindPhone?data=${JSON.stringify(data)}`)
  240. }
  241. } else if (!type) {
  242. if (buyerUser.ifFirst == 0) {
  243. uni.setStorageSync(J_STORAGE_KEY, buyerUser)
  244. this.bindSalesCustomer()
  245. const last_page = uni.getStorageSync('last_page') || ''
  246. if (last_page) {
  247. const last_page_options = uni.getStorageSync('last_page_options') || ''
  248. const str = JSON.stringify(last_page_options).replaceAll('{', '')
  249. .replaceAll('}', '')
  250. .replaceAll('"', '')
  251. .replaceAll(':', '=')
  252. .replaceAll(',', '&')
  253. // 保留其他路由,需跳转2下 到最后登录页面
  254. // uni.navigateBack({
  255. // delta: 1
  256. // })
  257. // uni.redirectTo({
  258. // url: `/${last_page}?${str}`,
  259. // })
  260. // 清空其他路由,直接跳转最后登录页面
  261. uni.reLaunch({
  262. url: `/${last_page}?${str}`
  263. })
  264. } else {
  265. // #ifdef MP-ALIPAY
  266. uni.navigateTo({
  267. url: '/pages/tabbar/user/index'
  268. })
  269. // #endif
  270. // #ifndef MP-ALIPAY
  271. this.$switchTab('/pages/tabbar/user/index')
  272. // #endif
  273. }
  274. } else { // 第一次登录,绑定手机号
  275. uni.redirectTo({
  276. url: 'bindPhone?data=' + JSON.stringify(data)
  277. })
  278. }
  279. }
  280. },
  281. bindSalesCustomer() {
  282. const shopId = uni.getStorageSync('shopId')
  283. const salesId = uni.getStorageSync('salesId')
  284. if (shopId && salesId && shopId !== '' && salesId !== '') {
  285. // 多次调用绑定方法,不提示任何信息即可
  286. NET.request(API.BindSalesCustomer, {
  287. shopId,
  288. distributorId: salesId
  289. }, 'POST').then((res) => {
  290. uni.removeStorageSync('salesId')
  291. uni.removeStorageSync('shopId')
  292. })
  293. }
  294. }
  295. }
  296. }
  297. </script>
  298. <style lang="scss" scoped>
  299. .container {
  300. background-color: #ce2601;
  301. min-height: 100vh;
  302. .otherLoginButton {
  303. margin-top: 195rpx;
  304. display: flex;
  305. flex-direction: column;
  306. align-items: center;
  307. .orderButtonItem {
  308. display: flex;
  309. justify-content: space-around;
  310. white-space: nowrap;
  311. &>view:not(:first-child) {
  312. padding-left: 48upx;
  313. }
  314. }
  315. }
  316. .agreement {
  317. margin-top: 120rpx;
  318. padding: 75upx;
  319. font-family: Source Han Sans CN;
  320. font-size: 24rpx;
  321. font-weight: 350;
  322. line-height: 32rpx;
  323. text-align: center;
  324. .colorText {
  325. color: #faf9f9;
  326. }
  327. }
  328. }
  329. .loginWxBut::after {
  330. border-radius: 0 !important;
  331. }
  332. </style>