金诚优选前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

346 lines
8.7 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. <template>
  2. <view>
  3. <lf-nav title="登录/注册" :showIcon="true" bgColor="#fff"></lf-nav>
  4. <view class="illustration">
  5. <image class="img" src="../../../static/images/empty.png" mode="aspectFill"></image>
  6. </view>
  7. <view class="fixed-btn">
  8. <button class="btn1" hover-class="lf-opacity" @click="toPhoneLogin">手机号登录</button>
  9. <button class="btn2" hover-class="lf-opacity" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">微信授权登录</button>
  10. </view>
  11. </view>
  12. </template>
  13. <script>
  14. import { config, wechat, GetUrl } from '@/common/js/utils.js';
  15. export default {
  16. data(){
  17. return {
  18. code: '',
  19. url: '',
  20. logo: '',
  21. author: config.PACKAGES.author,
  22. config: '',
  23. showAgreement: false,
  24. agreement: '',
  25. open_id: '',
  26. h5_url: ''
  27. }
  28. },
  29. onShow(){
  30. var token = this.$cookieStorage.get('user_token');
  31. // this.setData({
  32. // token: token
  33. // });
  34. this.token = token;
  35. var initInfo = this.$cookieStorage.get('init');
  36. if (initInfo && initInfo.shop_show_logo) {
  37. this.setData({
  38. logo: initInfo.shop_show_logo
  39. });
  40. }
  41. if (token) {
  42. // uni.switchTab({
  43. // url: '/pages/user/personal/personal'
  44. // });
  45. this.$url('/pages/user/my/center', {type: 'launch'})
  46. } else {
  47. // #ifdef MP-WEIXIN
  48. this.wxLogin();
  49. // #endif
  50. }
  51. },
  52. onLoad(e){
  53. // 第三方平台配置颜色
  54. var bgConfig = this.$cookieStorage.get('globalConfig') || '';
  55. this.setData({
  56. config: bgConfig
  57. });
  58. if (e.url) {
  59. this.url = decodeURIComponent(e.url);
  60. console.log('this.url',this.url)
  61. }
  62. if (e.shop_id) {
  63. this.$cookieStorage.set('shop_id', e.shop_id);
  64. }
  65. this.getGreementInfo();
  66. },
  67. methods: {
  68. // 获取用户协议
  69. getGreementInfo() {
  70. console.log("获取协议")
  71. // this.$http.get({
  72. // api: 'api/user/agreement'
  73. // }).then(res => {
  74. // if (res.statusCode == 200) {
  75. // res = res.data;
  76. // if (res.status) {
  77. // this.setData({
  78. // agreement: res.data
  79. // });
  80. // }
  81. // }
  82. // });
  83. },
  84. // 小程序静默登录
  85. wxLogin(){
  86. uni.showLoading({
  87. title: '正在自动登录',
  88. mask: true
  89. });
  90. uni.login({
  91. success: res => {
  92. console.log(res);
  93. if (res.code) {
  94. this.autoLogin(res.code);
  95. } else {
  96. this.$msg('获取code失败');
  97. }
  98. },
  99. });
  100. },
  101. // 通过code自动登录
  102. autoLogin(code) {
  103. this.$http.post({
  104. api: 'api/oauth/miniprogram/login',
  105. data: {
  106. code: code,
  107. open_type: 'miniprogram',
  108. shop_id: this.$cookieStorage.get('shop_id') || '',
  109. agent_code: this.$cookieStorage.get('coupon_agent_code') || this.$cookieStorage.get('agent_code') || '',
  110. clerk_id: this.$cookieStorage.get('clerk_id') || '',
  111. agent_code_time: this.$cookieStorage.get('agent_code_time') || '',
  112. shop_id_time: this.$cookieStorage.get('shop_id_time') || ''
  113. }
  114. }).then(res => {
  115. res = res.data;
  116. if (res.data && res.data.open_id) {
  117. this.setData({
  118. open_id: res.data.open_id
  119. });
  120. } // 如果接口返回token就直接登录,如果没有则弹出授权
  121. if (res.data.access_token) {
  122. console.log('已经返回给我了token');
  123. uni.hideLoading();
  124. var access_token = res.data.token_type + ' ' + res.data.access_token;
  125. var expires_in = res.data.expires_in || 315360000;
  126. this.$cookieStorage.set("user_token", access_token, expires_in);
  127. if (this.url) {
  128. uni.redirectTo({
  129. url: "/" + this.url,
  130. // fail: () => {
  131. // uni.switchTab({
  132. // url: "/" + this.url
  133. // });
  134. // }
  135. })
  136. } else {
  137. // uni.switchTab({
  138. // url: '/pages/user/personal/personal'
  139. // });
  140. this.$url('/pages/user/my/center', {type: 'launch'})
  141. }
  142. } else {
  143. uni.hideLoading();
  144. }
  145. }).catch(rej => {
  146. uni.hideLoading();
  147. uni.showModal({
  148. content: '请求失败,请重试',
  149. showCancel: false,
  150. success: res => {
  151. if (res.confirm || !res.cancel && !res.confirm) {
  152. this.wxLogin();
  153. }
  154. }
  155. });
  156. });
  157. },
  158. // 获取用户手机号
  159. getPhoneNumber(e) {
  160. if (e.detail.encryptedData) {
  161. uni.login({
  162. success: res => {
  163. if (res.code) {
  164. this.setData({
  165. code: res.code
  166. });
  167. this.phone(e);
  168. } else {
  169. uni.showModal({
  170. content: " 获取code失败",
  171. showCancel: false
  172. });
  173. }
  174. }
  175. });
  176. return;
  177. } else {
  178. this.toPhoneLogin();
  179. }
  180. },
  181. // 通过手机号登录注册
  182. phone(e) {
  183. uni.showLoading({
  184. title: '正在登录',
  185. mask: true
  186. });
  187. this.$http.post({
  188. api: 'api/oauth/miniprogram/mobile',
  189. data: {
  190. open_type: 'miniprogram',
  191. code: this.code,
  192. encryptedData: e.detail.encryptedData,
  193. iv: e.detail.iv,
  194. open_id: this.open_id,
  195. shop_id: this.$cookieStorage.get('shop_id') || '',
  196. agent_code: this.$cookieStorage.get('coupon_agent_code') || this.$cookieStorage.get('agent_code') || '',
  197. clerk_id: this.$cookieStorage.get('clerk_id') || '',
  198. agent_code_time: this.$cookieStorage.get('agent_code_time') || '',
  199. shop_id_time: this.$cookieStorage.get('shop_id_time') || ''
  200. }
  201. }).then(res => {
  202. if (res.statusCode == 200) {
  203. res = res.data;
  204. if (res.data.access_token) {
  205. var access_token = res.data.token_type + ' ' + res.data.access_token;
  206. var expires_in = res.data.expires_in || 315360000; // debugger;
  207. this.$cookieStorage.set("user_token", access_token, expires_in); // this.$cookieStorage.set("user_token",access_token,expires_in);
  208. // uni.setStorageSync("user_token",access_token);
  209. if (this.url) {
  210. // var path = ['pages/entity/store/store', 'pages/index/index/index',
  211. // 'pages/index/classification/classification', 'pages/store/tabCart/tabCart', 'pages/user/personal/personal',
  212. // 'pages/travels/index/index', 'pages/user/collar/collar'
  213. // ];
  214. // var pathIndex = path.indexOf(this.url);
  215. // if (pathIndex == -1) {
  216. // uni.redirectTo({
  217. // url: "/" + this.url
  218. // });
  219. // } else {
  220. // uni.switchTab({
  221. // url: "/" + this.url
  222. // });
  223. // }
  224. this.$url("/" + this.url, {type: 'redirect'});
  225. } else {
  226. // uni.switchTab({
  227. // url: '/pages/user/personal/personal'
  228. // });
  229. this.$url('/pages/user/my/center', {type: 'launch'})
  230. }
  231. } else {
  232. uni.showModal({
  233. content: res.message || '请求失败,请重试',
  234. showCancel: false
  235. });
  236. }
  237. } else {
  238. uni.showModal({
  239. content: '请求失败,请重试',
  240. showCancel: false
  241. });
  242. }
  243. uni.hideLoading();
  244. }).catch(rej => {
  245. uni.hideLoading();
  246. uni.showModal({
  247. content: '请求失败,请重试',
  248. showCancel: false
  249. });
  250. });
  251. },
  252. // 跳转到手机号验证码登录
  253. toPhoneLogin(){
  254. if (this.url) {
  255. // uni.navigateTo({
  256. // url: '/pages/user/loginType/loginType?url=' + encodeURIComponent(this.url) + '&open_id=' + this.open_id
  257. // });
  258. this.$url('/pages/user/loginType/loginType?url=' + encodeURIComponent(this.url) + '&open_id=' + this.open_id);
  259. } else {
  260. // wx.navigateTo({
  261. // url: '/pages/user/loginType/loginType?open_id=' + this.open_id
  262. // });
  263. this.$url('/pages/user/loginType/loginType?open_id=' + this.open_id);
  264. }
  265. },
  266. setData: function(obj) {
  267. let that = this;
  268. let keys = [];
  269. let val, data;
  270. Object.keys(obj).forEach(function(key) {
  271. keys = key.split('.');
  272. val = obj[key];
  273. data = that.$data;
  274. keys.forEach(function(key2, index) {
  275. if (index + 1 == keys.length) {
  276. that.$set(data, key2, val);
  277. } else {
  278. if (!data[key2]) {
  279. that.$set(data, key2, {});
  280. }
  281. }
  282. data = data[key2];
  283. });
  284. });
  285. }
  286. }
  287. }
  288. </script>
  289. <style lang="scss" scoped>
  290. .illustration{
  291. width: 750rpx;
  292. height: 750rpx;
  293. position: fixed;
  294. top: 44%;
  295. left: 0;
  296. margin-top: -375rpx;
  297. .img{
  298. width: 100%;
  299. height: 100%;
  300. }
  301. }
  302. .fixed-btn{
  303. width: 630rpx;
  304. height: max-content;
  305. position: fixed;
  306. left: calc(50% - 315rpx);
  307. top: calc(46% + 375rpx);
  308. display: flex;
  309. justify-content: space-between;
  310. .btn1, .btn2{
  311. width: 300rpx;
  312. height: 100rpx;
  313. border-radius: 60rpx;
  314. border: 1rpx solid #15716E;
  315. box-sizing: border-box;
  316. line-height: 100rpx;
  317. font-size: 32rpx;
  318. }
  319. .btn1{
  320. background-color: #F3F8F8;
  321. color: #15716E;
  322. }
  323. .btn2{
  324. background-color: #15716E;
  325. color: #FFFFFF;
  326. }
  327. }
  328. </style>