bindwechat.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. <template>
  2. <view class="container login">
  3. <view class="login-main">
  4. <text class="logo-text">您的微信尚未绑定机构账号,填写以下资料进行绑定后,您能通过微信快速登录。</text>
  5. </view>
  6. <view class="login-form clearfix">
  7. <view class="login-input">
  8. <input type="text"
  9. v-model="bindLinkName"
  10. maxlength="30"
  11. class="input"
  12. placeholder="请输入姓名"
  13. />
  14. </view>
  15. </view>
  16. <view class="login-form clearfix">
  17. <view class="login-input">
  18. <input type="number"
  19. v-model="bindLinkPhone"
  20. maxlength="11"
  21. class="input"
  22. placeholder="请输入手机号"
  23. />
  24. </view>
  25. </view>
  26. <view class="login-form clearfix">
  27. <view class="login-input code">
  28. <input type="number"
  29. v-model="imageCode"
  30. maxlength="6"
  31. class="input"
  32. placeholder="请输入右侧图形验证码"
  33. />
  34. </view>
  35. <view class="login-input img-btn">
  36. <view class="vscodeimg">
  37. <image :src="imageCodeUrl" mode=""></image>
  38. </view>
  39. <view class="vscod-refresh" @click.stop="getVerificationCode">
  40. <text class="iconfont icon-shuaxin"></text>
  41. <text class="ref-text">刷新</text>
  42. </view>
  43. </view>
  44. </view>
  45. <view class="login-form clearfix">
  46. <view class="login-input code">
  47. <input type="number"
  48. v-model="mobildeCode"
  49. maxlength="6"
  50. class="input"
  51. placeholder="请输入短信验证码"
  52. />
  53. </view>
  54. <view class="login-input btn" :class="[isMobileDisabled ? 'disabled' : '']" @click.stop="getMobileCodeFn">
  55. <button type="button"
  56. :disabled="isMobileDisabled"
  57. class="input"
  58. >{{ mobileCodeText }}</button>
  59. </view>
  60. </view>
  61. <view class="login-form clearfix">
  62. <view class="login-btn" @click="bindWechatInfo">绑定</view>
  63. </view>
  64. <!-- 授权按钮 -->
  65. <view class="model-warp" :class="[isUserInfo ? 'show':'none']">
  66. <view class="model-alert">
  67. <view class="alert-content">
  68. <view class="t-p">采美采购商城需要获取您的微信授权才能正常提供服务</view>
  69. </view>
  70. <view class="alert-btn">
  71. <view class="btn btn-cancel" @click="hideModel">取消</view>
  72. <button type="primary" size="small" open-type="getUserInfo" lang="zh_CN" @getuserinfo="getuserinfo" class="btn btn-confirm">授权</button>
  73. </view>
  74. </view>
  75. </view>
  76. </view>
  77. </template>
  78. <script>
  79. import { mapMutations } from 'vuex';
  80. import authorize from '@/common/config/authorize.js'
  81. import { bindingWechat } from "@/api/use.js"
  82. import { getImageCode, getbindWechatCode } from "@/api/utils.js"
  83. export default{
  84. data() {
  85. return{
  86. userID:'',
  87. bindMobile:'',
  88. bindLinkName:'', //用户姓名
  89. bindLinkPhone:'', //用户手机号
  90. mobildeCode:'', //手机验证码
  91. imageCode:'', //图形验证码
  92. imageCodeUrl:'', //图形验证码地址
  93. imageCodetoken:'', //图形校验token
  94. isMobileDisabled:false, //获取手机短信按钮
  95. isUserInfo:false, //控制显示授权弹窗
  96. count: '', //倒计时
  97. mobileCodeText: '获取验证码',
  98. codeTime: null,
  99. loginType:'', //跳转类型
  100. alertText:'',
  101. listType: '',
  102. listVal: '',
  103. detilType:'',
  104. id:'',//商品ID
  105. getOption:''
  106. }
  107. },
  108. onLoad(option) {
  109. let data = JSON.parse(option.data);
  110. this.codeType = option.codeType
  111. this.option = option
  112. this.loginType = data.type;
  113. this.id = data.id
  114. if(data.listType) {
  115. this.listType = data.listType;
  116. this.listVal = data.listVal;
  117. }
  118. this.getVerificationCode()
  119. },
  120. methods:{
  121. ...mapMutations(['login']),
  122. bindWechatInfo(){
  123. if( this.bindLinkName == ''){
  124. this.$util.msg('请输入姓名',2000)
  125. return
  126. }
  127. if( this.bindLinkPhone == ''){
  128. this.$util.msg('请输入手机号',2000)
  129. return
  130. }
  131. if(!this.$reg.isMobile(this.bindLinkPhone)){
  132. this.$util.msg('请输入正确的手机号',2000)
  133. return
  134. }
  135. if( this.mobildeCode == ''){
  136. this.$util.msg('请输入手机验证码',2000)
  137. return
  138. }
  139. if(!this.$reg.isMobileCode(this.mobildeCode)){
  140. this.$util.msg('验证码格式不正确',2000)
  141. return
  142. }
  143. //查看此微信用户是否已经授权过
  144. authorize.getSetting().then(res =>{
  145. // console.log('是否已授权',res);//0:为取消授权 1:为已授权 2:为未操作
  146. if(res == 2){
  147. this.isUserInfo = true
  148. }else{
  149. this.isUserInfo = false
  150. this.wxGetUserInfo()
  151. }
  152. })
  153. },
  154. getVerificationCode(){//图形验证
  155. getImageCode().then(res => {
  156. this.imageCodeUrl = res.data.baseImage
  157. this.imageCodetoken = res.data.token
  158. })
  159. },
  160. getMobileCodeFn(){//获取手机验证码
  161. let params = {
  162. mobile:this.bindLinkPhone,
  163. mobileOrEmail:this.bindMobile,
  164. platformType:2,
  165. imgCode:this.imageCode,
  166. token:this.imageCodetoken,
  167. }
  168. getbindWechatCode(params).then(res =>{
  169. const TIME_COUNT = 60;
  170. this.isMobileDisabled = true;
  171. this.$util.msg('验证短信已发送',2000)
  172.       if (!this.codeTime) {
  173.         this.count = TIME_COUNT;
  174.         this.codeTime = setInterval(() => {
  175.         if (this.count > 1 && this.count <= TIME_COUNT) {
  176.           this.count--
  177.           this.mobileCodeText = this.count +'s重新发送'
  178.          } else {
  179.           this.isMobileDisabled = false;
  180.           clearInterval(this.codeTime)
  181.           this.codeTime = null
  182. this.mobileCodeText = '获取验证码'
  183.          }
  184.         },1000)
  185.        }
  186. }).catch( res =>{
  187. this.$util.msg(res.msg,2000)
  188. })
  189. },
  190. getuserinfo: function (e) {//微信授权登录
  191. if (e.detail.userInfo) {
  192. this.wxGetUserInfo()
  193. }else{
  194. //用户按了拒绝按钮
  195. uni.showModal({
  196. content: '获取用户信息失败,请允许授权后才能继续使用小程序哦~',
  197. showCancel: false,
  198. confirmText: '授权',
  199. success: function (res) {
  200. if (res.confirm) {
  201. uni.openSetting({
  202. success: res => {
  203. // console.log(res.authSetting)
  204. },
  205. fail: res => {
  206. // console.log(res)
  207. }
  208. })
  209. }
  210. }
  211. })
  212. }
  213. },
  214. wxGetUserInfo(){
  215. let self = this
  216. authorize.getCode('weixin').then(wechatcode =>{
  217. wx.getUserInfo({
  218. success: res => {
  219. console.log('useInfo:',res.userInfo)
  220. self.userInfo = res.userInfo;
  221. let params ={
  222. userID:this.userID,
  223. mobile:this.bindLinkPhone,
  224. linkName:this.bindLinkName,
  225. verificationCode:this.mobildeCode,
  226. nickName:res.userInfo.nickName,
  227. headimgurl:res.userInfo.avatarUrl,
  228. }
  229. bindingWechat(params).then(res =>{
  230. this.isUserInfo = false;
  231. this.goUserLogininit()
  232. }).catch(res =>{
  233. this.$util.msg(res.msg,2000)
  234. })
  235. }
  236. });
  237. })
  238. },
  239. goUserLogininit(){
  240. let url;
  241. if(this.loginType) {
  242. if(this.loginType=='detilType'){
  243. this.$api.redirectTo(`/pages/goods/product?id=${this.id}&page=2`);
  244. }else if(this.loginType=='search'){
  245. this.$api.redirectTo('/pages/search/search');
  246. }else if(this.loginType == 1){
  247. url ='/pages/tabBar/cart/cart'
  248. }else if(this.loginType == 4){
  249. url ='/pages/tabBar/user/user'
  250. }else {
  251. url ='/pages/tabBar/home/home'
  252. }
  253. uni.switchTab({
  254. url
  255. })
  256. } else if(this.listType) {
  257. this.$api.navToListPage({type:this.listType,value:this.listVal,lType:'4'});
  258. }
  259. },
  260. //关闭未授权用户授权提示弹窗
  261. hideModel(){
  262. this.isUserInfo = false;
  263. }
  264. },
  265. onShow() {
  266. this.$api.getStorage().then((resolve) => {
  267. this.userID = resolve.userID
  268. this.bindMobile = resolve.bindMobile
  269. })
  270. }
  271. }
  272. </script>
  273. <style lang="scss">
  274. .login{
  275. width: 100%;
  276. height: auto;
  277. border-top: 1px solid #F7F7F7;
  278. .model-warp.none{
  279. display: none;
  280. }
  281. .model-warp.show{
  282. display: block;
  283. }
  284. .login-main{
  285. width: 702rpx;
  286. background: rgba(225, 86, 22, 0.1);
  287. display: flex;
  288. flex-direction: column;
  289. align-items: center;
  290. height: 68rpx;
  291. padding: 20rpx 24rpx;
  292. margin: 24rpx 0 118rpx 0;
  293. .logo-text{
  294. font-size: 24rpx;
  295. line-height: 34rpx;
  296. color: $color-system;
  297. }
  298. }
  299. .login-form{
  300. width: 702rpx;
  301. height: auto;
  302. padding: 0 24rpx;
  303. .login-input{
  304. width: 654rpx;
  305. height: 40rpx;
  306. padding: 24rpx;
  307. margin-bottom: 20rpx;
  308. background: #F7F7F7;
  309. border-radius: 14rpx;
  310. display: flex;
  311. flex-direction: column;
  312. align-items: center;
  313. &.code{
  314. width: 370rpx;
  315. float: left;
  316. margin-right: 20rpx;
  317. }
  318. &.btn{
  319. width: 210rpx;
  320. float: left;
  321. background: $btn-confirm;
  322. .input{
  323. color: #FFFFFF;
  324. background: $btn-confirm;
  325. }
  326. &.disabled{
  327. background: #F7F7F7;
  328. .input{
  329. background: #F7F7F7;
  330. color: #999999;
  331. }
  332. }
  333. }
  334. &.img-btn{
  335. width: 250rpx;
  336. height: 88rpx;
  337. padding: 0;
  338. float: left;
  339. background: #FFFFFF;
  340. display: block;
  341. .vscodeimg{
  342. width: 180rpx;
  343. height: 88rpx;
  344. float: left;
  345. display: flex;
  346. flex-direction: column;
  347. align-items: center;
  348. border-radius: 14rpx;
  349. image{
  350. width: 180rpx;
  351. height: 88rpx;
  352. border-radius: 14rpx;
  353. }
  354. }
  355. .vscod-refresh{
  356. width: 70rpx;
  357. float: right;
  358. display: flex;
  359. flex-direction: column;
  360. align-items: center;
  361. .icon-shuaxin{
  362. font-size: 48rpx;
  363. color: #333333;
  364. }
  365. .ref-text{
  366. font-size: 24rpx;
  367. color: #333333;
  368. }
  369. }
  370. }
  371. .input{
  372. width: 100%;
  373. height: 100%;
  374. background: #F7F7F7;
  375. font-size: $font-size-28;
  376. line-height: 40rpx;
  377. color: #333333;
  378. border-radius: 14rpx;
  379. }
  380. }
  381. }
  382. .login-btn{
  383. width: 702rpx;
  384. height: 88rpx;
  385. border-radius: 14rpx;
  386. font-size: $font-size-28;
  387. line-height: 88rpx;
  388. color: #FFFFFF;
  389. margin: 0 auto;
  390. text-align: center;
  391. background: $btn-confirm;
  392. margin-top: 100rpx;
  393. }
  394. .model-authorization{
  395. width: 100%;
  396. height: 100%;
  397. position: fixed;
  398. top: 0;
  399. left: 0;
  400. z-index: 999;
  401. .authorization{
  402. width: 518rpx;
  403. height: 320rpx;
  404. position: absolute;
  405. background: rgba(255,255,255,.7);
  406. left: 0;
  407. right: 0;
  408. bottom: 0;
  409. top: 0;
  410. margin: auto;
  411. .to-btn{
  412. position: absolute;
  413. top: 0;
  414. left: 0;
  415. right: 0;
  416. bottom: 0;
  417. margin: auto;
  418. width: 70%;
  419. height: 88rpx;
  420. font-size: $font-size-28;
  421. line-height: 88rpx;
  422. color: #FFFFFF;
  423. text-align: center;
  424. border-radius: 44rpx;
  425. }
  426. }
  427. }
  428. }
  429. </style>