index.vue 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. <template>
  2. <mobile-frame>
  3. <view class="main">
  4. <view class="one" v-if="platform.uniPlatform=='app'">
  5. <uni-forms ref="form" :rules="rules" :model="form" label-width="auto">
  6. <uni-forms-item label="手机号" name="phone">
  7. <uni-easyinput type="text" v-model="form.phone" placeholder="请输入手机号" />
  8. </uni-forms-item>
  9. <uni-forms-item label="验证码" name="code">
  10. <uni-easyinput type="text" v-model="form.code" placeholder="请输入验证码" />
  11. <button type="default" size="mini">验证码</button>
  12. </uni-forms-item>
  13. <view class="btn">
  14. <button type="default" size="mini" @click="toSubmit('form')">提交登录</button>
  15. </view>
  16. </uni-forms>
  17. </view>
  18. <view class="two" v-else-if="platform.uniPlatform=='mp-weixin'">
  19. <view class="icon">
  20. <text class="iconfont icon-weixin"></text>
  21. </view>
  22. <view class="btn">
  23. <button type="default" size="mini" @tap="wxLogin()">微信信任登录</button>
  24. </view>
  25. </view>
  26. <view class="thr">
  27. <view class="thr_1">
  28. <checkbox-group @change="changeAgree">
  29. <label>
  30. <checkbox :checked="agree" />
  31. <text @tap="toAgree()">我已阅读并同意“用户协议”和“隐私政策”</text>
  32. </label>
  33. </checkbox-group>
  34. </view>
  35. </view>
  36. </view>
  37. <uni-popup ref="dialogShow" type="dialog">
  38. <uni-popup-dialog :title="dialog.title" mode="base" :before-close="true" @confirm="diaSubmit(dialog)" @close="diaReset(dialog)">
  39. <view class="dialog dialog_1" v-if="dialog.type=='1'">
  40. <uni-forms ref="phoneForm" :model="phoneForm" label-width="auto">
  41. <uni-forms-item label="方式">
  42. <uni-data-checkbox v-model="phoneForm.type" :localdata="typeList" />
  43. </uni-forms-item>
  44. <uni-forms-item label="微信" v-if="phoneForm.type==0">
  45. <button type="default" size="mini" open-type="getPhoneNumber" @getphonenumber="getUserPhone">获取微信绑定手机</button>
  46. </uni-forms-item>
  47. <uni-forms-item label="手机号" v-if="phoneForm.type==0">
  48. <uni-easyinput type="number" v-model="phoneForm.phone" disabled />
  49. </uni-forms-item>
  50. <uni-forms-item label="手机号" v-if="phoneForm.type==1">
  51. <view class="yzm">
  52. <view class="l">
  53. <uni-easyinput type="number" v-model="phoneForm.phone" placeholder="请输入手机号" />
  54. </view>
  55. <view class="r">
  56. <button type="default" size="mini" @tap="sendCount">{{time_count==0?'验证码':time_count}}</button>
  57. </view>
  58. </view>
  59. </uni-forms-item>
  60. <uni-forms-item label="验证码" v-if="phoneForm.type==1">
  61. <uni-easyinput type="number" v-model="phoneForm.code" placeholder="请输入验证码" />
  62. </uni-forms-item>
  63. </uni-forms>
  64. </view>
  65. </uni-popup-dialog>
  66. </uni-popup>
  67. </mobile-frame>
  68. </template>
  69. <script>
  70. export default {
  71. data() {
  72. return {
  73. // 平台信息
  74. platform: {},
  75. //openid
  76. openid: '',
  77. // 隐私协议
  78. agree: false,
  79. form: {},
  80. rules: {
  81. phone: {
  82. rules: [{
  83. required: true,
  84. errorMessage: '请输入手机号',
  85. },
  86. {
  87. minLength: 11,
  88. maxLength: 11,
  89. errorMessage: '账号长度在{maxLength}个字符',
  90. }
  91. ]
  92. },
  93. code: {
  94. rules: [{
  95. required: true,
  96. errorMessage: '请输入验证码',
  97. }, ]
  98. },
  99. },
  100. // 微信登陆
  101. // 无账号
  102. is_user: false,
  103. user: {},
  104. // 弹框
  105. dialog: {
  106. title: '绑定手机号',
  107. type: '1'
  108. },
  109. // 绑定手机号
  110. phoneForm: {
  111. type: 0,
  112. },
  113. typeList: [ //
  114. {
  115. text: '微信',
  116. value: 0
  117. },
  118. {
  119. text: '短信验证',
  120. value: 1
  121. }
  122. ],
  123. // 倒计时
  124. time_count: 0
  125. };
  126. },
  127. onShow: function() {
  128. const that = this;
  129. that.search();
  130. },
  131. methods: {
  132. search() {
  133. const that = this;
  134. uni.getStorage({
  135. key: 'system',
  136. success: function(res) {
  137. if (res.data) that.$set(that, `platform`, res.data);
  138. // 获取openid
  139. that.searchOpenid()
  140. }
  141. })
  142. },
  143. // 查询openid
  144. searchOpenid() {
  145. const that = this;
  146. if (that.platform.uniPlatform == 'mp-weixin') {
  147. uni.login({
  148. provider: 'weixin',
  149. success: async function(res) {
  150. const aee = await that.$api(`/wechat/api/login/app`, 'GET', {
  151. js_code: res.code,
  152. config: 'pointApp'
  153. })
  154. if (aee.errcode == '0') {
  155. that.$set(that, `openid`, aee.data.openid);
  156. // 微信登录
  157. that.openidLogin(aee.data.openid);
  158. }
  159. },
  160. fail: function(err) {
  161. console.log(err);
  162. }
  163. })
  164. } else if (that.platform.uniPlatform == 'app') {
  165. console.log('to do');
  166. }
  167. },
  168. // 有账号,微信登录,直接返回上一页
  169. async openidLogin(e) {
  170. const that = this;
  171. let res = await that.$api(`/user/wxLogin`, 'POST', {
  172. openid: e
  173. })
  174. if (res.errcode == '0') {
  175. uni.setStorage({
  176. data: res.data,
  177. key: 'token',
  178. success: function() {
  179. uni.navigateBack({
  180. delta: 1
  181. })
  182. }
  183. })
  184. } else {
  185. if (res.errcode == '-5') {
  186. console.log('无账号');
  187. that.$set(that, `is_user`, false)
  188. }
  189. }
  190. },
  191. // 微信信任登录
  192. async wxLogin() {
  193. const that = this;
  194. if (that.agree) {
  195. uni.getUserProfile({
  196. desc: '用于展示',
  197. success: async function(res) {
  198. let parmas = {
  199. openid: that.openid,
  200. icon: [{
  201. url: res.userInfo.avatarUrl
  202. }],
  203. name: res.userInfo.nickName,
  204. }
  205. const arr = await that.$api(`/user`, 'POST', parmas);
  206. if (arr.errcode == '0') {
  207. that.$set(that, `user`, arr.data);
  208. // 打开弹框
  209. that.diaShow();
  210. } else {
  211. uni.showToast({
  212. title: arr.errmsg,
  213. icon: 'none'
  214. });
  215. }
  216. },
  217. fail: function(err) {
  218. console.log(err);
  219. }
  220. })
  221. } else {
  222. uni.showToast({
  223. title: '请阅读并同意用户协议和隐私政策',
  224. icon: 'none'
  225. })
  226. }
  227. },
  228. // 微信信任登录-1
  229. async wxLogins() {
  230. const that = this;
  231. const agg = await that.$api(`/user/wxLogin`, 'POST', {
  232. openid: that.openid
  233. })
  234. if (agg.errcode == '0') {
  235. uni.setStorage({
  236. data: agg.data,
  237. key: 'token',
  238. success: function() {
  239. uni.navigateBack({
  240. delta: 1
  241. })
  242. }
  243. })
  244. } else {
  245. uni.showToast({
  246. title: agg.errmsg,
  247. icon: 'none'
  248. });
  249. }
  250. },
  251. // 打开弹框,绑定手机号
  252. diaShow() {
  253. const that = this;
  254. that.$refs.dialogShow.open();
  255. },
  256. // 获取手机号
  257. async getUserPhone(e) {
  258. const that = this;
  259. let user = that.user;
  260. let detail = e.detail;
  261. let res = await that.$api(`/wechat/api/login/getPhone`, 'GET', {
  262. config: 'pointApp',
  263. code: detail.code
  264. })
  265. if (res.errcode == '0') {
  266. let phone = res.data && res.data.phone_info && res.data.phone_info.purePhoneNumber || '';
  267. that.$set(that.phoneForm, `phone`, phone);
  268. } else {
  269. uni.showToast({
  270. title: res.errmsg,
  271. icon: 'none'
  272. })
  273. }
  274. },
  275. // 短信验证
  276. // 获取验证码
  277. sendCount() {
  278. const that = this;
  279. let form = that.phoneForm;
  280. if (form && form.phone) {
  281. let time_count = 60;
  282. that.$set(that, `time_count`, time_count);
  283. that.timeDown();
  284. } else {
  285. uni.showToast({
  286. title: '输入错误,请重新输入!',
  287. icon: 'none'
  288. })
  289. }
  290. },
  291. // 倒计时
  292. timeDown() {
  293. const that = this;
  294. var times = setInterval(() => {
  295. that.time_count--;
  296. if (that.time_count <= 0) {
  297. clearInterval(times);
  298. }
  299. }, 1000);
  300. },
  301. // 弹框确认(关闭)
  302. async diaSubmit(e) {
  303. const that = this;
  304. let user = this.user;
  305. if (e.type == '1') {
  306. const that = this;
  307. let form = that.phoneForm;
  308. if (form.type == '0') {
  309. if (form && form.phone) that.updatePhone(form.phone);
  310. } else if (form.type == '1') {
  311. let code = '1234';
  312. if (form.code == code) {
  313. if (form && form.phone) that.updatePhone(form.phone);
  314. } else {
  315. uni.showToast({
  316. title: '短信验证失败,请重新输入!',
  317. icon: 'none'
  318. })
  319. }
  320. }
  321. }
  322. },
  323. async updatePhone(e) {
  324. const that = this;
  325. let user = that.user;
  326. let arr = await that.$api(`/user/${user._id}`, 'POST', {
  327. phone: e
  328. })
  329. if (arr.errcode == '0') {
  330. that.wxLogins();
  331. } else {
  332. uni.showToast({
  333. title: res.errmsg,
  334. icon: 'none'
  335. })
  336. }
  337. },
  338. // 弹框取消(关闭)
  339. diaReset(e) {
  340. const that = this;
  341. if (e.type == '1') {
  342. that.wxLogins();
  343. }
  344. },
  345. // 同意隐私协议
  346. changeAgree() {
  347. const that = this;
  348. if (that.agree) that.$set(that, `agree`, false)
  349. else that.$set(that, `agree`, true);
  350. },
  351. // 查看隐私协议
  352. toAgree() {
  353. uni.navigateTo({
  354. url: `/pages/other/agree`
  355. })
  356. },
  357. }
  358. }
  359. </script>
  360. <style lang="scss">
  361. .main {
  362. .one {
  363. padding: 2vw;
  364. .btn {
  365. text-align: center;
  366. margin: 0 0 2vw 0;
  367. button {
  368. margin: 0 2vw;
  369. background-color: var(--f35BColor);
  370. color: var(--fffColor);
  371. }
  372. }
  373. }
  374. .two {
  375. text-align: center;
  376. margin: 20vw 0;
  377. .icon {
  378. margin: 0 0 5vw 0;
  379. text {
  380. font-size: 60px;
  381. }
  382. }
  383. .btn {
  384. button {
  385. background-color: var(--f35BColor);
  386. color: var(--fffColor);
  387. }
  388. }
  389. }
  390. .thr {
  391. .thr_1 {
  392. padding: 0 2vw;
  393. font-size: 14px;
  394. text-align: center;
  395. position: absolute;
  396. width: 96vw;
  397. bottom: 25vw;
  398. }
  399. }
  400. }
  401. .dialog {
  402. width: 100vw;
  403. }
  404. .dialog_1 {
  405. .uni-forms {
  406. display: flex;
  407. form {
  408. width: 100%;
  409. }
  410. }
  411. }
  412. .uni-data-checklist {
  413. border: 1px solid #E5E5E5;
  414. padding: 1vw;
  415. border-radius: 5px;
  416. }
  417. .uni-forms-item {
  418. margin-bottom: 2vw !important;
  419. }
  420. .yzm {
  421. display: flex;
  422. flex-direction: row;
  423. justify-content: space-between;
  424. .l {
  425. flex-grow: 1;
  426. }
  427. .r {
  428. width: 18vw;
  429. button {
  430. width: 100%;
  431. line-height: 2.8;
  432. padding: 0 1vw;
  433. }
  434. }
  435. }
  436. </style>