bindwechat.vue 11 KB

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