login.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <template>
  2. <view class="container flex-items-plus flex-column">
  3. <global-loading />
  4. <view class="login-logoBox">
  5. <image class="login-logo" src="../../static/images/origin/loginLogo.png"></image>
  6. </view>
  7. <view class="loginTitle-text flex-column-plus flex-items font-color-666 fs26">
  8. <text>为了给您提供更好的服务</text>
  9. <text>我们需要您的授权哦~</text>
  10. </view>
  11. <view>
  12. <!-- #ifdef MP-WEIXIN -->
  13. <button class="loginWxBut fs28" @click="GetUserInfo">微信登录</button>
  14. <button class="goToHome fs28" @click="goToHome">去首页逛逛</button>
  15. <!-- #endif -->
  16. <!-- #ifdef MP-ALIPAY -->
  17. <button class="loginWxBut fs28" @click="GetAliPayUserInfo">支付宝登录</button>
  18. <!-- #endif -->
  19. <!-- #ifndef MP-ALIPAY -->
  20. <view v-if="noMp" class="loginBut" @click="goLogin">手机号登录</view>
  21. <!-- #endif -->
  22. </view>
  23. <view v-if="noMp" class="flex-row-plus mar-top-30">
  24. <text class="font-color-C5AA7B">还没有账号,</text>
  25. <view class="register-text" @click="goRegister">去注册</view>
  26. </view>
  27. </view>
  28. </template>
  29. <script>
  30. const NET = require('../../utils/request')
  31. const API = require('../../config/api')
  32. export default {
  33. data() {
  34. return {
  35. WXloginQuery: {
  36. 'code': '',
  37. 'salesId': 0,
  38. 'headImage': '',
  39. 'terminal': 1
  40. },
  41. noMp: false
  42. // beforePage: undefined,
  43. // doubleBeforePage: undefined
  44. }
  45. },
  46. onLoad(options) {
  47. // #ifndef MP
  48. this.noMp = true
  49. // #endif
  50. const pages = getCurrentPages()
  51. uni.setStorageSync('last_page', pages[pages.length - 2].route)
  52. if (pages[pages.length - 2].options) {
  53. uni.setStorageSync('last_page_options', pages[pages.length - 2].options)
  54. }
  55. },
  56. onShow() {
  57. // 所有需要授权页面进登录页面取消loading
  58. uni.hideLoading()
  59. },
  60. onBackPress() {
  61. uni.switchTab({
  62. url: '../../pages/tabbar/index/index'
  63. })
  64. return true
  65. },
  66. methods: {
  67. back() {
  68. uni.switchTab({
  69. url: '../../pages/tabbar/index/index'
  70. })
  71. },
  72. goLogin() {
  73. uni.navigateTo({
  74. url: 'accountLogin'
  75. })
  76. },
  77. goRegister() {
  78. uni.navigateTo({
  79. url: 'register'
  80. })
  81. },
  82. // 微信登录
  83. GetUserInfo() {
  84. // uni.showLoading({
  85. // title: '正在加载...',
  86. // mask: true
  87. // });
  88. const that = this
  89. // // #ifdef APP-PLUS
  90. // uni.login({
  91. // provider: 'weixin',
  92. // success: function(loginRes) {
  93. // // 获取用户信息
  94. // uni.getUserInfo({
  95. // provider: 'weixin',
  96. // success: function(infoRes) {
  97. // let wechatOpenId = infoRes.userInfo.openId
  98. // let avatarUrl = infoRes.userInfo.avatarUrl
  99. // NET.request(API.WxAppLogin, {
  100. // 'wechatOpenId': wechatOpenId
  101. // }, 'POST').then(res => {
  102. // let data = {
  103. // wechatOpenId: wechatOpenId,
  104. // headImage: avatarUrl
  105. // }
  106. // that.loginSuc(res.data, data)
  107. // }).catch(res => {
  108. // console.log('WxAppLogin failed: ', res)
  109. // })
  110. // },
  111. // fail: () => {
  112. // uni.showToast({
  113. // title: "微信登录授权失败",
  114. // icon: "none"
  115. // });
  116. // }
  117. // });
  118. // }
  119. // });
  120. // // #endif
  121. // #ifdef MP-WEIXIN
  122. uni.login({
  123. provider: 'weixin',
  124. success: (res2) => {
  125. uni.hideLoading()
  126. that.WXloginQuery.code = res2.code
  127. },
  128. fail: () => {
  129. uni.showToast({
  130. title: '微信登录授权失败',
  131. icon: 'none'
  132. })
  133. }
  134. })
  135. uni.getUserProfile({
  136. desc: '正在获取', // 不写不弹提示框
  137. success(res) {
  138. that.WXloginQuery.headImage = res.userInfo.avatarUrl
  139. NET.request(API.WxLogin, that.WXloginQuery, 'POST').then((res) => {
  140. const data = {
  141. wechatOpenId: res.data.wechatOpenId,
  142. headImage: res.data.headImage
  143. }
  144. that.loginSuc(res.data, data)
  145. })
  146. .catch((err) => {
  147. })
  148. },
  149. fail(err) {
  150. uni.showToast({
  151. title: '微信登录授权失败',
  152. icon: 'none'
  153. })
  154. }
  155. })
  156. /* uni.getUserInfo({
  157. provider: 'weixin',
  158. success: function(infoRes) {
  159. },
  160. fail: () => {
  161. uni.showToast({
  162. title: "微信登录授权失败",
  163. icon: "none"
  164. });
  165. }
  166. }); */
  167. /* uni.getUserProfile({
  168. desc: '正在获取', //不写不弹提示框
  169. success: function(res) {
  170. console.dir(res)
  171. that.WXloginQuery.headImg = res.userInfo.avatarUrl
  172. // that.loginSuc({'ifFirst': 1}, {'headImg': res.userInfo.avatarUrl})
  173. },
  174. fail: function(err) {
  175. uni.showToast({
  176. title: "微信登录授权失败",
  177. icon: "none"
  178. });
  179. }
  180. }) */
  181. // #endif
  182. },
  183. GetAliPayUserInfo() {
  184. const that = this
  185. uni.login({
  186. provider: 'alipay',
  187. scopes: 'auth_user',
  188. success(loginRes) {
  189. that.alipayLogin(loginRes.authCode)
  190. }
  191. })
  192. },
  193. alipayLogin(authCode) {
  194. NET.request(API.AlipayLogin, {
  195. 'code': authCode
  196. }, 'POST').then((res) => {
  197. this.loginSuc(res.data, {
  198. 'buyerUserId': res.data.buyerUserId
  199. })
  200. })
  201. .catch((err) => {
  202. })
  203. },
  204. goToHome() {
  205. uni.switchTab({
  206. url: '/pages/tabbar/index/index'
  207. })
  208. },
  209. loginSuc(buyerUser, data) {
  210. if (buyerUser.ifFirst == 0) {
  211. uni.setStorageSync('storage_key', buyerUser)
  212. this.bindSalesCustomer()
  213. /* uni.switchTab({
  214. url: '/pages/tabbar/user/index',
  215. success() {
  216. uni.hideLoading()
  217. }
  218. }) */
  219. const last_page = uni.getStorageSync('last_page') || ''
  220. if (last_page) {
  221. const last_page_options = uni.getStorageSync('last_page_options') || ''
  222. const str = JSON.stringify(last_page_options).replaceAll('{', '')
  223. .replaceAll('}', '')
  224. .replaceAll('"', '')
  225. .replaceAll(':', '=')
  226. .replaceAll(',', '&')
  227. // 保留其他路由,需跳转2下 到最后登录页面
  228. // uni.navigateBack({
  229. // delta: 1
  230. // })
  231. // uni.redirectTo({
  232. // url: `/${last_page}?${str}`,
  233. // // url: `/${last_page}?` + str
  234. // })
  235. // 清空其他路由,直接跳转最后登录页面
  236. uni.reLaunch({
  237. url: `/${last_page}?${str}`
  238. // url: `/${last_page}?` + str
  239. })
  240. } else {
  241. // #ifdef MP-ALIPAY
  242. uni.navigateTo({
  243. url: '/pages/tabbar/user/index'
  244. })
  245. // #endif
  246. // #ifndef MP-ALIPAY
  247. uni.switchTab({
  248. url: '/pages/tabbar/user/index'
  249. })
  250. // #endif
  251. }
  252. } else { // 第一次登录,绑定手机号
  253. uni.redirectTo({
  254. url: 'bindPhone?data=' + JSON.stringify(data)
  255. })
  256. }
  257. },
  258. bindSalesCustomer() {
  259. const shopId = uni.getStorageSync('shopId')
  260. const salesId = uni.getStorageSync('salesId')
  261. if (shopId && salesId && shopId !== '' && salesId !== '') {
  262. // 多次调用绑定方法,不提示任何信息即可
  263. NET.request(API.BindSalesCustomer, {
  264. shopId,
  265. distributorId: salesId
  266. }, 'POST').then((res) => {
  267. uni.removeStorageSync('salesId')
  268. uni.removeStorageSync('shopId')
  269. })
  270. .catch((err) => {
  271. console.dir(err)
  272. })
  273. }
  274. }
  275. }
  276. }
  277. </script>
  278. <style lang="scss">
  279. .container {
  280. background-color: #FFFFFF;
  281. height: 100vh;
  282. .login-logoBox {
  283. margin-top: -200rpx;
  284. .login-logo {
  285. width: 200rpx;
  286. height: 167rpx;
  287. }
  288. }
  289. .loginTitle-text {
  290. margin-top: 100rpx;
  291. }
  292. .goToHome {
  293. background: none;
  294. border: none;
  295. width: auto;
  296. margin-top: 50rpx;
  297. color: #999999;
  298. text-decoration: underline;
  299. &:after {
  300. display: none;
  301. }
  302. }
  303. .loginWxBut {
  304. background: #333333;
  305. color: #FFEBC4;
  306. height: 88rpx;
  307. width: 600rpx;
  308. text-align: center;
  309. line-height: 88rpx;
  310. margin-top: 49rpx;
  311. }
  312. .loginBut {
  313. background-color: #333333;
  314. color: #FFEBC4;
  315. height: 88rpx;
  316. width: 600rpx;
  317. text-align: center;
  318. line-height: 88rpx;
  319. margin-top: 30rpx;
  320. }
  321. .register-text {
  322. color: #C5AA7B;
  323. }
  324. }
  325. </style>
  326. <style scoped>
  327. .loginWxBut::after {
  328. border-radius: 0 !important;
  329. }
  330. </style>