index.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <template>
  2. <view class="content" :style="[mainStyle]">
  3. <view class="title">
  4. <view>手机验证登录</view>
  5. <text>首次登录将自动注册为用户</text>
  6. </view>
  7. <view class="form"><input v-model="mobile" type="number" maxlength="11" placeholder="请输入手机号码"
  8. placeholder-class="placeholder" class="ipt" /></view>
  9. <view class="form ddflex">
  10. <input v-model="code" type="number" maxlength="6" placeholder="请输入短信验证码" placeholder-class="placeholder"
  11. class="ipt flex" />
  12. <view class="yzm" @click="getCode()" v-text="sendMsg"></view>
  13. </view>
  14. <view class="btn" @click="submitLogin">登录</view>
  15. <view class="agree ddflex" @click="agree">
  16. <image :src="'../../static/pages/images/' + (isAgree ? 'gou1_h.png' : 'gou1.png')" class="gou"></image>
  17. 已阅读并同意
  18. <text @click.stop="jumpUrl('/mine/page/page?title=服务协议&isXieyi=true')">《服务协议》</text>
  19. <text @click.stop="jumpUrl('/mine/page/page?title=隐私政策&isYinsi=true')">《隐私政策》</text>
  20. </view>
  21. <!-- ios app存在第三方授权登录的必须接入苹果登录,故先屏蔽该功能;只在安卓上做-->
  22. <view class="other" v-if="is_weixn()">
  23. <view class="tits ddflex">其他方式登录</view>
  24. <view class="wechat" @click="watchLogin()">
  25. <image src="../../static/pages/images/wechat.png"></image>
  26. </view>
  27. </view>
  28. </view>
  29. </template>
  30. <script>
  31. const app = getApp();
  32. const req = require('../../utils/request.js');
  33. const api = require('../../utils/api.js');
  34. const util = require('../../utils/util.js');
  35. const jump = require('../../utils/jump.js');
  36. export default {
  37. data() {
  38. return {
  39. mainStyle: app.globalData.mainStyle,
  40. isAgree: false,
  41. isGetCodeDisabled: false,
  42. mobile: '', //手机号
  43. code: '', //code
  44. sendMsg: '获取验证码',
  45. typeRole: '',
  46. userInfo: {},
  47. platform: '', //android、ios
  48. iosPassed: 0, //IOSAPP应用市场是否过审开关 0否 1是
  49. isShowWXView: false //是否显示微信授权登录控件
  50. };
  51. },
  52. onLoad() {
  53. req.removeStorage('authorize_jump');
  54. this.platform = uni.getSystemInfoSync().platform;
  55. this.iosPassed = JSON.parse(req.getStorage('configRes')).iosPassed;
  56. this.checkShowView();
  57. var pages = getCurrentPages();
  58. console.log('页面的栈>>>>',pages);
  59. // switch () {
  60. // case 'android':
  61. // console.log('运行Android上');
  62. // break;
  63. // case 'ios':
  64. // console.log('运行iOS上');
  65. // break;
  66. // default:
  67. // console.log('运行在开发者工具上');
  68. // break;
  69. // }
  70. },
  71. methods: {
  72. // 是微信浏览器
  73. is_weixn() {
  74. var ua = navigator.userAgent.toLowerCase();
  75. if (ua.match(/MicroMessenger/i) == 'micromessenger') {
  76. return true;
  77. } else {
  78. return false;
  79. }
  80. },
  81. jumpUrl(url) {
  82. uni.navigateTo({
  83. url: url
  84. });
  85. },
  86. //校验是否可显示vip控件
  87. checkShowView() {
  88. if (this.platform == 'android') {
  89. this.isShowWXView = true;
  90. } else if (this.platform == 'ios' && this.iosPassed == 1) {
  91. this.isShowWXView = true;
  92. }
  93. },
  94. agree() {
  95. this.isAgree = !this.isAgree;
  96. },
  97. getCode() {
  98. if (this.isGetCodeDisabled == true) return false;
  99. if (!this.mobile) {
  100. req.msg('请输入手机号');
  101. return;
  102. }
  103. if (!util.isMobile(this.mobile)) {
  104. req.msg('请输入11位有效手机号');
  105. return;
  106. }
  107. req.postRequest(
  108. api.sms_login, {
  109. mobile: this.mobile
  110. },
  111. data => {
  112. req.msg('验证码获取成功');
  113. let time = 60;
  114. let interval = setInterval(() => {
  115. time--;
  116. if (time == 0) {
  117. this.isGetCodeDisabled = false;
  118. this.sendMsg = '获取验证码';
  119. clearInterval(interval);
  120. } else {
  121. this.isGetCodeDisabled = true;
  122. this.sendMsg = time + '秒后再获取';
  123. }
  124. }, 1000);
  125. },
  126. true
  127. );
  128. },
  129. submitLogin: function() {
  130. var dataP = {};
  131. var apiUrl = '';
  132. if (!this.mobile) {
  133. req.msg('请输入手机号');
  134. return;
  135. }
  136. if (!this.code) {
  137. req.msg('请输入验证码');
  138. return;
  139. }
  140. if (!this.isAgree) {
  141. return req.msg('请勾选协议');
  142. }
  143. // dataP.loginMethod = 'me';
  144. dataP = {
  145. mobile: this.mobile,
  146. code: this.code
  147. };
  148. console.log('数据>>>>', dataP);
  149. req.postRequest(
  150. api.mobile_login,
  151. dataP,
  152. data => {
  153. console.log('登录完成》》》》', data);
  154. this.userInfo = data;
  155. req.setStorage('AUTH_TOKEN', data.token);
  156. req.setStorage('userInfo', data);
  157. this.savePushCid();
  158. // if (data.hasOwnProperty('isNewRegister')) {
  159. // if (data.isNewRegister) {
  160. // util.onEventRegister('phone');
  161. // }
  162. // } else {
  163. // util.onEventRegister('phone');
  164. // }
  165. // util.onEventLogin('phone');
  166. jump.loginToNext(this.userInfo);
  167. },
  168. true
  169. );
  170. },
  171. /**
  172. * 用户关联个推的设备cid
  173. */
  174. savePushCid() {
  175. var cid = req.getStorage('pushCid');
  176. if (cid) {
  177. req.postRequest('/api/equipmentCode/save', {
  178. userType: 1,
  179. equipmentCode: cid
  180. }, data => {});
  181. }
  182. },
  183. watchLogin() {
  184. var tha = this;
  185. if (!this.isAgree) {
  186. return req.msg('请勾选协议');
  187. }
  188. // #ifdef APP-PLUS
  189. uni.login({
  190. provider: 'weixin',
  191. success: function(loginRes) {
  192. console.log('登录数据>>>>', loginRes);
  193. var dataP = {
  194. accessToken: loginRes.authResult.access_token,
  195. openId: loginRes.authResult.openid,
  196. unionId: loginRes.authResult.unionid
  197. };
  198. console.log('微信登录数据>>>>', dataP);
  199. req.postRequest(
  200. api.wechat_login,
  201. dataP,
  202. data => {
  203. tha.userInfo = data;
  204. req.setStorage('AUTH_TOKEN', data.token);
  205. req.setStorage('userInfo', data);
  206. tha.savePushCid();
  207. // util.onEventLogin('wechat');
  208. jump.loginToNext(tha.userInfo);
  209. console.log('微信返回>>>>', data);
  210. },
  211. true
  212. );
  213. }
  214. });
  215. // #endif
  216. // #ifdef H5
  217. //公众号
  218. getApp().wxOauth('', '','snsapi_userinfo');
  219. // var url =
  220. // 'https://open.weixin.qq.com/connect/oauth2/authorize' +
  221. // '?appid=' +
  222. // req.public.WX_GZH_APPID +
  223. // '&redirect_uri=' +
  224. // encodeURIComponent(req.public.WX_GZH_URI) +
  225. // '&response_type=code' +
  226. // '&scope=snsapi_userinfo' +
  227. // '&state=STATE#wechat_redirect';
  228. // window.location.href = url;
  229. // #endif
  230. }
  231. }
  232. };
  233. </script>
  234. <style>
  235. @import './index.css';
  236. </style>