bindPhone.vue 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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/logo/ju-icon.png"></image>
  6. </view>
  7. <view class="loginInfo fs26 font-color-999">
  8. <p>共建真实、安全的交易环境</p>
  9. <p>本平台承诺不会滥用您授权的信息</p>
  10. </view>
  11. <view v-show="verifyType === 0" class="verifyTypeBox">
  12. <view class="verifyItem" @click="verifyType = 1">
  13. 短信验证码
  14. </view>
  15. <view class="verifyItem verify-checked" @click="verifyType = 2">
  16. 小程序授权
  17. </view>
  18. </view>
  19. <view v-if="verifyType == 1" class="phoneVerify">
  20. <view class="iphoneNum-box flex-row-plus flex-items">
  21. <view style="margin-right: 30rpx">
  22. <image class="loginIcon" src="../../static/images/origin/phone.png"></image>
  23. </view>
  24. <view>
  25. <input
  26. v-model="phone" maxlength="11" placeholder-class="iphoneNum-input" type="number"
  27. placeholder="请输入您的手机号"
  28. />
  29. </view>
  30. </view>
  31. <view class="flex-row-plus mar-top-20">
  32. <view class="code-box">
  33. <view style="margin-right: 30rpx">
  34. <image class="loginIcon" src="../../static/images/origin/code.png"></image>
  35. </view>
  36. <view>
  37. <input v-model="code" maxlength="6" placeholder-class="codeNum-input" placeholder="请输入验证码" />
  38. </view>
  39. </view>
  40. <view :class="disabled === true ? 'on' : ''" :disabled="disabled" class="getcode" @click="codede">
  41. {{ text }}
  42. </view>
  43. </view>
  44. <view class="mar-top-60">
  45. <view class="registerBut mar-top-100" :class="{ bindCls: phone !== '' && code !== '' }" @click="bindPhone">
  46. 确认
  47. </view>
  48. </view>
  49. <!-- <u-popup v-model="bindPhoneShow" class="popupbind" mode="center" border-radius="14" @close="closepopupbind"
  50. :closeable="true" close-icon-pos="top-right" close-icon-size="20">
  51. <view class="popupbind-box flex-items-plus">
  52. <view class="flex-column flex-items-plus" style="width: 95%;">
  53. <view class="fa38 font-color-C5AA7B">提示</view>
  54. <view class="mar-top-50" style="width: 90%;">此手机号已经绑定其他微信号,暂时无法与您的微信账号进行绑定。</view>
  55. </view>
  56. </view>
  57. </u-popup> -->
  58. </view>
  59. <view v-if="verifyType == 2" class="mpVerify">
  60. <button
  61. v-if="mpAlipay" class="verifyPhone" open-type="getAuthorize" scope="phoneNumber"
  62. @getAuthorize="onGetAuthorize" @error="onAuthError"
  63. >
  64. 授权手机号验证
  65. </button>
  66. <button class="verifyPhone fs28 mar-top-100" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">
  67. 微信手机号授权
  68. </button>
  69. </view>
  70. <view v-if="verifyType !== 0" class="backBtn" @click="verifyType = 0">返回</view>
  71. </view>
  72. </template>
  73. <script>
  74. import sendVerifyCode from '@/mixins/SendVerifyCode'
  75. import { J_STORAGE_KEY } from '../../config/constant'
  76. const NET = require('../../utils/request')
  77. const API = require('../../config/api')
  78. import { getVerifyCodeApi } from '../../api/user'
  79. export default {
  80. name: 'BindPhone',
  81. mixins: [ sendVerifyCode ],
  82. data() {
  83. return {
  84. disabled: false,
  85. phone: '',
  86. code: '',
  87. wechatOpenId: '',
  88. headImage: '',
  89. bindPhoneShow: false,
  90. verifyType: 0,
  91. buyerUserId: 0,
  92. isMp: false,
  93. mpAlipay: false,
  94. mpWechat: false
  95. }
  96. },
  97. onLoad(options) {
  98. console.log(options)
  99. if (options.data) {
  100. const bindPhoneData = JSON.parse(options.data)
  101. this.wechatOpenId = bindPhoneData.wechatOpenId
  102. this.headImage = bindPhoneData.headImage
  103. this.buyerUserId = bindPhoneData.buyerUserId
  104. }
  105. // #ifdef MP
  106. this.isMp = true
  107. // #endif
  108. // #ifdef MP-ALIPAY
  109. this.mpAlipay = true
  110. // #endif
  111. // #ifdef MP-WEIXIN
  112. this.mpWechat = true
  113. // #endif
  114. },
  115. methods: {
  116. closepopupbind() {
  117. this.bindPhoneShow = false
  118. },
  119. codede() {
  120. const phoneCodeVerification = /^[1][3-9][0-9]{9}$/
  121. if (this.phone == '') {
  122. uni.showToast({
  123. title: '请输入手机号!',
  124. duration: 2000,
  125. icon: 'none'
  126. })
  127. } else if (!phoneCodeVerification.test(this.phone)) {
  128. uni.showToast({
  129. title: '请输入正确的手机号!',
  130. duration: 2000,
  131. icon: 'none'
  132. })
  133. } else {
  134. getVerifyCodeApi({
  135. phone: this.phone
  136. }).then((res) => {
  137. this.sendCode()
  138. })
  139. .catch((res) => {
  140. uni.showToast({
  141. title: res.data.message,
  142. duration: 2000,
  143. icon: 'none'
  144. })
  145. })
  146. }
  147. },
  148. bindPhone() {
  149. // #ifdef MP-ALIPAY
  150. this.updateAliPhone(this.phone, false)
  151. // #endif
  152. // #ifdef MP-WEIXIN || H5
  153. this.updateWxPhone()
  154. // #endif
  155. },
  156. onGetAuthorize() {
  157. const that = this
  158. // uni.showLoading({
  159. // mask: true,
  160. // title: '验证中...',
  161. // })
  162. this.$showLoading()
  163. my.getPhoneNumber({
  164. success: (res) => {
  165. this.$hideLoading()
  166. const encryptedData = res.response
  167. that.updateAliPhone(encryptedData, true)
  168. },
  169. fail: (res) => {
  170. this.$hideLoading()
  171. uni.hideLoading()
  172. uni.showToast({
  173. title: '验证失败',
  174. icon: 'none'
  175. })
  176. }
  177. })
  178. },
  179. onAuthError() {
  180. },
  181. getPhoneNumber(e) {
  182. if (e.detail.errMsg === 'getPhoneNumber:ok') {
  183. uni.login({
  184. provider: 'weixin',
  185. success: (res) => {
  186. NET.request(API.GetSessionKey, {
  187. code: res.code
  188. }, 'POST').then((res1) => {
  189. NET.request(API.SetWxPhone, {
  190. wechatOpenId: res1.data.wechatOpenId,
  191. sessionKey: res1.data.sessionKey,
  192. encryptedData: e.detail.encryptedData,
  193. iv: e.detail.iv,
  194. headImage: this.headImage
  195. }, 'POST').then((res2) => {
  196. const item = res2.data
  197. uni.setStorageSync(J_STORAGE_KEY, item)
  198. this.bindSalesCustomer()
  199. this.gotoHome()
  200. })
  201. })
  202. },
  203. fail: () => {
  204. uni.showToast({
  205. title: '微信登录授权失败',
  206. icon: 'none'
  207. })
  208. }
  209. })
  210. } else {
  211. uni.showToast({
  212. title: '获取手机授权失败',
  213. icon: 'none'
  214. })
  215. }
  216. },
  217. updateWxPhone() {
  218. NET.request(API.UpdateWxPhone, {
  219. phone: this.phone,
  220. wechatOpenId: this.wechatOpenId,
  221. headImage: this.headImage,
  222. verificationCode: this.code,
  223. channelCode: this.channelCode,
  224. terminal: 3
  225. }, 'POST').then((res) => {
  226. const item = res.data
  227. uni.setStorageSync(J_STORAGE_KEY, item)
  228. this.bindSalesCustomer()
  229. uni.showToast({
  230. title: '绑定成功',
  231. duration: 2000,
  232. icon: 'none'
  233. })
  234. this.gotoHome()
  235. })
  236. .catch((res) => {
  237. this.bindPhoneShow = true
  238. })
  239. },
  240. updateAliPhone(encryptedData, encrypted) {
  241. NET.request(API.UpdateAliPhone, {
  242. 'phone': encryptedData,
  243. 'buyerUserId': this.buyerUserId,
  244. encrypted
  245. }, 'POST').then((res) => {
  246. const item = res.data
  247. uni.setStorageSync(J_STORAGE_KEY, item)
  248. this.bindSalesCustomer()
  249. this.phone = item.phone
  250. uni.showToast({
  251. title: '绑定成功',
  252. duration: 2000,
  253. icon: 'none'
  254. })
  255. this.gotoHome()
  256. })
  257. .finally((res) => {
  258. uni.hideLoading()
  259. })
  260. },
  261. bindSalesCustomer() {
  262. const shopId = uni.getStorageSync('shopId')
  263. const salesId = uni.getStorageSync('salesId')
  264. if (shopId && salesId) {
  265. // 多次调用绑定方法,不提示任何信息即可
  266. NET.request(API.BindSalesCustomer, {
  267. shopId,
  268. distributorId: salesId
  269. }, 'POST').then((res) => {
  270. uni.removeStorageSync('salesId')
  271. uni.removeStorageSync('shopId')
  272. })
  273. .catch((res) => {
  274. console.dir(res)
  275. })
  276. }
  277. },
  278. gotoHome() {
  279. // #ifdef MP-ALIPAY
  280. uni.navigateTo({
  281. url: '/pages/tabbar/user/index'
  282. })
  283. // #endif
  284. // #ifndef MP-ALIPAY
  285. this.$switchTab('/pages/tabbar/user/index')
  286. // #endif
  287. }
  288. }
  289. }
  290. </script>
  291. <style lang="scss">
  292. .container {
  293. background-color: #FFFFFF;
  294. height: 100vh;
  295. .login-logoBox {
  296. margin-top: 100upx;
  297. .login-logo {
  298. width: 234upx;
  299. height: 193upx;
  300. }
  301. }
  302. .loginInfo {
  303. margin: 130rpx 0 50rpx 0;
  304. text-align: center;
  305. }
  306. .verifyTypeBox {
  307. width: 98%;
  308. .verifyItem {
  309. width: 80%;
  310. display: block;
  311. text-align: center;
  312. border: 2upx solid #F3F4F5;
  313. padding: 30upx;
  314. font-size: 34upx;
  315. margin: 0 auto 30rpx auto;
  316. color: #C5AA7B;
  317. }
  318. .verify-checked {
  319. color: white;
  320. background: #C5AA7B;
  321. }
  322. }
  323. .phoneVerify {
  324. height: 450upx;
  325. .iphoneNum-box {
  326. border-bottom: 1rpx solid #DDDDDD;
  327. height: 100rpx;
  328. width: 600rpx;
  329. .loginIcon {
  330. width: 40rpx;
  331. height: 53rpx;
  332. }
  333. .iphoneNum-input {
  334. color: #999999;
  335. font-size: 28rpx;
  336. font-weight: 400;
  337. }
  338. }
  339. .passwordNum-box {
  340. border-bottom: 1rpx solid #DDDDDD;
  341. height: 100rpx;
  342. width: 600rpx;
  343. .passwordNum-input {
  344. color: #999999;
  345. font-size: 28rpx;
  346. font-weight: 400;
  347. width: 346rpx;
  348. }
  349. }
  350. .code-box {
  351. border-bottom: 1rpx solid #DDDDDD;
  352. height: 100rpx;
  353. width: 360rpx;
  354. display: flex;
  355. flex-direction: row;
  356. justify-content: space-between;
  357. align-items: center;
  358. .loginIcon {
  359. width: 44rpx;
  360. height: 50rpx;
  361. }
  362. .code-lab {
  363. width: 200rpx;
  364. }
  365. .codeNum-input {
  366. color: #999999;
  367. font-size: 28rpx;
  368. font-weight: 400;
  369. }
  370. }
  371. .getcode {
  372. background-color: #C5AA7B;
  373. height: 100rpx;
  374. width: 230rpx;
  375. display: flex;
  376. flex-direction: row;
  377. justify-content: center;
  378. align-items: center;
  379. margin-left: 20rpx;
  380. color: #FFFFFF;
  381. }
  382. .registerBut {
  383. background: #F3F4F5;
  384. color: #999999;
  385. height: 100rpx;
  386. width: 600rpx;
  387. text-align: center;
  388. line-height: 100rpx;
  389. margin-top: 30rpx;
  390. }
  391. .bindCls {
  392. background: #333333;
  393. color: #F5DEB2;
  394. }
  395. }
  396. .mpVerify {
  397. height: 300upx;
  398. .verifyPhone {
  399. background: #333333;
  400. color: #FFEBC4;
  401. height: 100upx;
  402. width: 600upx;
  403. text-align: center;
  404. line-height: 100upx;
  405. margin-top: 88upx;
  406. border-radius: 0;
  407. }
  408. }
  409. .backBtn {
  410. color: #C5AA7B;
  411. text-decoration: underline;
  412. }
  413. }
  414. </style>