dt_login.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <template>
  2. <FootDialog direction="center" :config="config" :isTitle="false" :isRadius="false" :visible.sync="dialogShow"
  3. @close="$emit('close')">
  4. <!-- #ifdef MP-WEIXIN -->
  5. <template slot="main">
  6. <view class="dt-login-wrap">
  7. <view class="title">您还没登录</view>
  8. <view class="subtitle">请先登录再进行操作</view>
  9. <image class="login-icon" src="/static/common/login.png"></image>
  10. <view class="btn-wrap">
  11. <button class="btn-base"
  12. hover-class="button-hover"
  13. @tap="hide">暂不登录
  14. </button>
  15. <button class=" btn-base "
  16. style="color: #2f7ff5;"
  17. hover-class="button-hover"
  18. open-type="getPhoneNumber"
  19. @getphonenumber="tapGetPhoneNumber">立即登录
  20. </button>
  21. </view>
  22. </view>
  23. </template>
  24. <!-- #endif -->
  25. <!-- #ifdef APP-PLUS -->
  26. <template slot="main">
  27. <view class="dt-login-wrap">
  28. <view class="title">登 录</view>
  29. <view>
  30. <view>
  31. <text>账号:</text>
  32. <input v-model="formData.account" type="number" placeholder="请输入账号"/>
  33. </view>
  34. <view class="line"></view>
  35. <view>
  36. <text>密码:</text>
  37. <input v-model="formData.passwd" placeholder="请输入密码"/>
  38. </view>
  39. </view>
  40. <view class="btn-wrap">
  41. <button class="btn-base dt-color-primary"
  42. hover-class="button-hover"
  43. @tap="touristLogin">登 录
  44. </button>
  45. </view>
  46. </view>
  47. </template>
  48. <!-- #endif -->
  49. </FootDialog>
  50. </template>
  51. <script>
  52. import FootDialog from './foot_dialog.vue'
  53. import {setTimeout} from 'timers';
  54. export default {
  55. components: {
  56. FootDialog
  57. },
  58. data() {
  59. return {
  60. config: {
  61. mainBgColor: 'transparent'
  62. },
  63. dialogShow: false,
  64. respWx: {},
  65. respWx2: {},
  66. respLogin: {},
  67. timer: null,
  68. inviteCode: null,
  69. formData: {
  70. account: '',
  71. passwd: ''
  72. },
  73. formRules: {
  74. account: [{
  75. required: true,
  76. message: '请输入账号'
  77. }, {
  78. type: 'mobile',
  79. message: '手机号码有误!'
  80. }],
  81. passwd: [{
  82. required: true,
  83. message: '请输入密码'
  84. }]
  85. }
  86. }
  87. },
  88. methods: {
  89. show() {
  90. console.log("show");
  91. let that = this
  92. this.inviteCode = this.$auth.getInviteCode()
  93. console.log("inviteCode", this.inviteCode);
  94. this.dialogShow = true;
  95. this.$mpi.wxLogin().then(res => {
  96. this.respWx = res
  97. console.log("res", res);
  98. })
  99. this.setTimer()
  100. },
  101. hide() {
  102. console.log("hide");
  103. if (this.timer) {
  104. clearTimeout(this.timer)
  105. this.timer = null
  106. console.log('clearTimeout')
  107. }
  108. this.dialogShow = false;
  109. },
  110. async setTimer() {
  111. console.log("setTimer");
  112. this.respWx = await this.$mpi.wxLogin()
  113. this.timer = setTimeout(() => {
  114. this.setTimer()
  115. }, 240000) // 每隔4分钟重新请求 jscode
  116. },
  117. async login(detail) {
  118. console.log("2:login", detail);
  119. try {
  120. this.$dialog.showLoading()
  121. if (!this.respLogin.sessionKey) {
  122. this.respLogin = await this.$api.loginByCode({
  123. _isReject: true,
  124. code: this.respWx.code
  125. })
  126. }
  127. console.log(this.respLogin);
  128. let resp = await this.$api.loginByWxapp({
  129. _isReject: true,
  130. openId: this.respLogin.openId,
  131. sessionKey: this.respLogin.sessionKey,
  132. encryptedData: detail.encryptedData,
  133. iv: detail.iv,
  134. inviteCode: this.inviteCode
  135. })
  136. console.log(56, resp)
  137. resp.openId = this.respLogin.openId
  138. resp.sessionKey = this.respLogin.sessionKey
  139. let userType = this.$global.userType.member
  140. this.$auth.login(userType, resp.sessionId, resp.userId, resp)
  141. this.hide()
  142. // this.$auth.setInviteCode(null);
  143. this.$auth.removeInviteCode();
  144. let a = this.$auth.getInviteCode();
  145. if (this.inviteCode) {
  146. this.$emit('getDistributorCoupon')
  147. } else {
  148. this.$emit('signIn', resp)
  149. }
  150. } catch (err) {
  151. // console.log(72,err)
  152. this.$dialog.alert({
  153. content: err.errmsg || '服务繁忙~'
  154. })
  155. } finally {
  156. this.$dialog.hideLoading()
  157. if (this.timer) {
  158. clearTimeout(this.timer)
  159. this.setTimer()
  160. }
  161. }
  162. },
  163. /**
  164. * app 登陆逻辑
  165. */
  166. async touristLogin() {
  167. console.log("");
  168. if (!this.validate(this.formRules, this.formData)) {
  169. return
  170. }
  171. try {
  172. this.$dialog.showLoading()
  173. if (!this.respLogin.sessionKey) {
  174. this.respLogin = await this.$api.loginByCode({
  175. _isReject: true,
  176. code: this.respWx.code
  177. })
  178. }
  179. let resp = await this.$api.touristLogin({
  180. account: this.formData.account,
  181. passwd: this.formData.passwd
  182. })
  183. console.log(resp)
  184. let userType = this.$global.userType.member
  185. resp.openId = this.respLogin.openId
  186. resp.sessionKey = this.respLogin.sessionKey
  187. this.$auth.login(userType, resp.sessionId, resp.userId, resp)
  188. this.hide()
  189. this.$auth.removeInviteCode();
  190. let a = this.$auth.getInviteCode();
  191. if (this.inviteCode) {
  192. this.$emit('getDistributorCoupon')
  193. } else {
  194. this.$emit('signIn', resp)
  195. }
  196. } catch (err) {
  197. // console.log(72,err)
  198. this.$dialog.alert({
  199. content: err.errmsg || '服务繁忙~'
  200. })
  201. } finally {
  202. this.$dialog.hideLoading()
  203. if (this.timer) {
  204. clearTimeout(this.timer)
  205. this.setTimer()
  206. }
  207. }
  208. },
  209. /**1 :点击登陆
  210. * @param {Object} e
  211. */
  212. async tapGetPhoneNumber(e) {
  213. console.log(1, "点击登陆")
  214. // console.log(48,e.detail)
  215. if (e.detail.errMsg != 'getPhoneNumber:ok') {
  216. return
  217. }
  218. // let authSetting = await this.$mpi.wxGetSetting()
  219. // if(authSetting['scope.userInfo']) {
  220. // let userInfo = await this.$mpi.wxGetUserInfo()
  221. // console.log(56,userInfo)
  222. // }
  223. // console.log(55,authSetting)
  224. this.login(e.detail)
  225. },
  226. },
  227. beforeDestroy() {
  228. if (this.timer) {
  229. clearTimeout(this.timer)
  230. this.timer = null
  231. console.log('clearTimeout')
  232. }
  233. }
  234. }
  235. </script>
  236. <style lang="scss">
  237. button {
  238. font-size: 28 rpx;
  239. }
  240. .dt-login-wrap {
  241. margin: 0 auto;
  242. display: flex;
  243. flex-direction: column;
  244. align-items: center;
  245. width: 560 upx;
  246. background-color: #fff;
  247. border-radius: 16 upx;
  248. .title {
  249. padding-top: 60 upx;
  250. padding-bottom: 28 upx;
  251. font-size: 32 upx;
  252. font-weight: bold;
  253. }
  254. .subtitle {
  255. color: #7e7e7e;
  256. font-size: 26 upx;
  257. }
  258. .login-icon {
  259. margin: 25 upx auto 52 upx;
  260. width: 320 upx;
  261. height: 340 upx;
  262. }
  263. .btn-wrap {
  264. display: flex;
  265. width: 100%;
  266. line-height: 80 upx;
  267. padding: 8 rpx 0 rpx;
  268. font-size: 24 rpx;
  269. border-top: 1 upx solid #f2f2f2;
  270. .btn-base {
  271. padding: 0 60 rpx;
  272. flex: 1;
  273. text-align: center;
  274. color: #66656a;
  275. border-radius: 0;
  276. }
  277. .btn-base:before {
  278. position: absolute;
  279. top: 0;
  280. right: 1 upx;
  281. width: 100%;
  282. height: 100%;
  283. border-right: 1 upx solid #ebebeb;
  284. content: "";
  285. }
  286. .btn-base:last-child:before {
  287. display: none;
  288. content: "";
  289. }
  290. }
  291. }
  292. </style>