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 { mapState,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.$util.msg('验证短信已发送',2000)
  171.       if (!this.codeTime) {
  172.         this.count = TIME_COUNT;
  173. this.isMobileDisabled = true;
  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. authorize.getCode('weixin').then(wechatcode =>{
  216. wx.getUserInfo({
  217. success: res => {
  218. console.log('useInfo:',res.userInfo)
  219. this.isUserInfo = false;
  220. this.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(response =>{
  230. // this.login(response.data);
  231. uni.switchTab({
  232. url:'/pages/tabBar/user/user'
  233. })
  234. }).catch(response =>{
  235. this.$util.msg(response.msg,2000)
  236. })
  237. }
  238. });
  239. })
  240. },
  241. goUserLogininit(){
  242. let url;
  243. if(this.loginType) {
  244. if(this.loginType=='detilType'){
  245. this.$api.redirectTo(`/pages/goods/product?id=${this.id}&page=2`);
  246. }else if(this.loginType=='search'){
  247. this.$api.redirectTo('/pages/search/search');
  248. }else if(this.loginType == 1){
  249. url ='/pages/tabBar/cart/cart'
  250. }else if(this.loginType == 4){
  251. url ='/pages/tabBar/user/user'
  252. }else {
  253. url ='/pages/tabBar/home/home'
  254. }
  255. } else if(this.listType) {
  256. this.$api.navToListPage({type:this.listType,value:this.listVal,lType:'4'});
  257. }
  258. },
  259. //关闭未授权用户授权提示弹窗
  260. hideModel(){
  261. this.isUserInfo = false;
  262. }
  263. },
  264. onShow() {
  265. this.$api.getStorage().then((resolve) => {
  266. this.userID = resolve.userID
  267. this.bindMobile = resolve.bindMobile
  268. })
  269. }
  270. }
  271. </script>
  272. <style lang="scss">
  273. .login{
  274. width: 100%;
  275. height: auto;
  276. border-top: 1px solid #F7F7F7;
  277. .model-warp.none{
  278. display: none;
  279. }
  280. .model-warp.show{
  281. display: block;
  282. }
  283. .login-main{
  284. width: 702rpx;
  285. background: rgba(225, 86, 22, 0.1);
  286. display: flex;
  287. flex-direction: column;
  288. align-items: center;
  289. height: 68rpx;
  290. padding: 20rpx 24rpx;
  291. margin: 24rpx 0 118rpx 0;
  292. .logo-text{
  293. font-size: 24rpx;
  294. line-height: 34rpx;
  295. color: $color-system;
  296. }
  297. }
  298. .login-form{
  299. width: 702rpx;
  300. height: auto;
  301. padding: 0 24rpx;
  302. .login-input{
  303. width: 654rpx;
  304. height: 40rpx;
  305. padding: 24rpx;
  306. margin-bottom: 20rpx;
  307. background: #F7F7F7;
  308. border-radius: 14rpx;
  309. display: flex;
  310. flex-direction: column;
  311. align-items: center;
  312. &.code{
  313. width: 370rpx;
  314. float: left;
  315. margin-right: 20rpx;
  316. }
  317. &.btn{
  318. width: 210rpx;
  319. float: left;
  320. background: $btn-confirm;
  321. .input{
  322. color: #FFFFFF;
  323. background: $btn-confirm;
  324. }
  325. &.disabled{
  326. background: #F7F7F7;
  327. .input{
  328. background: #F7F7F7;
  329. color: #999999;
  330. }
  331. }
  332. }
  333. &.img-btn{
  334. width: 250rpx;
  335. height: 88rpx;
  336. padding: 0;
  337. float: left;
  338. background: #FFFFFF;
  339. display: block;
  340. .vscodeimg{
  341. width: 180rpx;
  342. height: 88rpx;
  343. float: left;
  344. display: flex;
  345. flex-direction: column;
  346. align-items: center;
  347. border-radius: 14rpx;
  348. image{
  349. width: 180rpx;
  350. height: 88rpx;
  351. border-radius: 14rpx;
  352. }
  353. }
  354. .vscod-refresh{
  355. width: 70rpx;
  356. float: right;
  357. display: flex;
  358. flex-direction: column;
  359. align-items: center;
  360. .icon-shuaxin{
  361. font-size: 48rpx;
  362. color: #333333;
  363. }
  364. .ref-text{
  365. font-size: 24rpx;
  366. color: #333333;
  367. }
  368. }
  369. }
  370. .input{
  371. width: 100%;
  372. height: 100%;
  373. background: #F7F7F7;
  374. font-size: $font-size-28;
  375. line-height: 40rpx;
  376. color: #333333;
  377. border-radius: 14rpx;
  378. }
  379. }
  380. }
  381. .login-btn{
  382. width: 702rpx;
  383. height: 88rpx;
  384. border-radius: 14rpx;
  385. font-size: $font-size-28;
  386. line-height: 88rpx;
  387. color: #FFFFFF;
  388. margin: 0 auto;
  389. text-align: center;
  390. background: $btn-confirm;
  391. margin-top: 100rpx;
  392. }
  393. .model-authorization{
  394. width: 100%;
  395. height: 100%;
  396. position: fixed;
  397. top: 0;
  398. left: 0;
  399. z-index: 999;
  400. .authorization{
  401. width: 518rpx;
  402. height: 320rpx;
  403. position: absolute;
  404. background: rgba(255,255,255,.7);
  405. left: 0;
  406. right: 0;
  407. bottom: 0;
  408. top: 0;
  409. margin: auto;
  410. .to-btn{
  411. position: absolute;
  412. top: 0;
  413. left: 0;
  414. right: 0;
  415. bottom: 0;
  416. margin: auto;
  417. width: 70%;
  418. height: 88rpx;
  419. font-size: $font-size-28;
  420. line-height: 88rpx;
  421. color: #FFFFFF;
  422. text-align: center;
  423. border-radius: 44rpx;
  424. }
  425. }
  426. }
  427. }
  428. </style>