index.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // index.js
  2. // 获取应用实例
  3. const app = getApp()
  4. const util = require('../../utils/util.js');
  5. const user = require('../../utils/user.js');
  6. const api = require('../../api/api.js');
  7. const map = require('../../utils/map.js');
  8. Page({
  9. data: {
  10. userInfo: {},
  11. mobile: '',
  12. captcha: '', //验证码
  13. code: '' //小程序登录code
  14. },
  15. onLoad() {
  16. let userInfo = wx.getStorageSync('userInfo');
  17. if (userInfo && userInfo.type == 1) return; //小哥登录 不验证地理位置
  18. this.getLoginCode();
  19. // map.getCity().then(res => {}).catch(res => {
  20. // if (res == 3) {
  21. // wx.showModal({
  22. // title: '提醒',
  23. // showCancel: false,
  24. // content: '当前区域,无服务门店'
  25. // })
  26. // } else {
  27. // wx.showModal({
  28. // title: '提醒',
  29. // showCancel: false,
  30. // content: '您未授权,部分功能将受限'
  31. // })
  32. // }
  33. // })
  34. },
  35. goShelter() {
  36. wx.navigateTo({
  37. url: '/pages/shelter/index',
  38. })
  39. },
  40. onShow() {
  41. let that = this;
  42. //验证是否登陆过
  43. user.checkLogin().then(res => {
  44. app.globalData.hasLogin = true;
  45. wx.switchTab({
  46. url: '/pages/upgrade/serviceOrder/serviceOrder',
  47. })
  48. }).catch(res => {
  49. that.getLoginCode();
  50. })
  51. },
  52. getLoginCode() {
  53. let that = this;
  54. user.login().then(res => {
  55. console.info(res)
  56. that.setData({
  57. code: res.code
  58. })
  59. })
  60. },
  61. getPhoneNumber: function (e) {
  62. var ivObj = e.detail.iv
  63. var telObj = e.detail.encryptedData
  64. var that = this;
  65. //------执行Login---------
  66. util.request(api.AuthRegister, {
  67. code: that.data.code,
  68. encryptedData: telObj,
  69. iv: ivObj,
  70. loginType: 1
  71. },'POST').then(function (res) {
  72. console.info(res)
  73. if (res.errno === 0) {
  74. //存储用户信息
  75. console.info("登录成功")
  76. app.globalData.hasLogin = true;
  77. wx.setStorageSync('userInfo', res.data.userInfo);
  78. wx.setStorageSync('token', res.data.token);
  79. that.setData({
  80. mobile: res.data.mobile
  81. })
  82. wx.showToast({
  83. title: '授权注册成功',
  84. icon: 'success',
  85. duration: 2000,
  86. complete: function () {
  87. // if(res.data.userInfo.type==1)
  88. // wx.navigateTo({
  89. // url: '/pages/pre/index',
  90. // })
  91. // else
  92. // wx.navigateTo({
  93. // url: '/pages/servant/servant',
  94. // })
  95. wx.switchTab({
  96. url: '/pages/upgrade/serviceOrder/serviceOrder',
  97. })
  98. }
  99. })
  100. } else {
  101. console.info(res);
  102. that.getLoginCode();
  103. wx.showToast({
  104. title: res.errmsg,
  105. duration: 3000,
  106. icon:'none'
  107. });
  108. }
  109. });
  110. //-----------------是否授权,授权通过进入主页面,授权拒绝则停留在登陆界面
  111. if (e.detail.errMsg == 'getPhoneNumber:fail user deny') { //用户点击拒绝
  112. wx.showModal({
  113. title: '警告',
  114. content: '您点击了拒绝授权,功能无法使用!!!',
  115. showCancel: false,
  116. confirmText: '确定',
  117. success: function (res) {
  118. // 用户没有授权成功,不需要改变 isHide 的值
  119. if (res.confirm) {
  120. wx.switchTab({
  121. url: '/pages/index/index',
  122. })
  123. };
  124. }
  125. })
  126. }
  127. },
  128. getUserProfile(e) {
  129. let that = this
  130. // 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认,开发者妥善保管用户快速填写的头像昵称,避免重复弹窗
  131. wx.getUserProfile({
  132. desc: '展示用户信息', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  133. success: (res) => {
  134. console.log(res)
  135. this.setData({
  136. userInfo: res.userInfo,
  137. hasUserInfo: true
  138. })
  139. //登录
  140. util.request(api.AuthLoginByCaptcha, {
  141. mobile: that.data.mobile,
  142. captcha: that.data.captcha,
  143. userInfo: res.userInfo,
  144. code: that.data.code
  145. }, "POST").then(res => {
  146. if (res.errno == 0) {
  147. //存储用户信息
  148. console.info("登录成功")
  149. app.globalData.hasLogin = true;
  150. wx.setStorageSync('userInfo', res.data.userInfo);
  151. wx.setStorageSync('token', res.data.token);
  152. that.setData({
  153. mobile: res.data.mobile
  154. })
  155. wx.showToast({
  156. title: '授权注册成功',
  157. icon: 'success',
  158. duration: 2000,
  159. complete: function () {
  160. wx.switchTab({
  161. url: '/pages/upgrade/serviceOrder/serviceOrder',
  162. })
  163. }
  164. })
  165. } else {
  166. wx.showToast({
  167. title: res.errmsg,
  168. duration: 3000
  169. });
  170. }
  171. }).catch(res => {
  172. })
  173. }
  174. })
  175. },
  176. getCaptcha() {
  177. console.info(this.data.mobile)
  178. if (this.data.mobile.length != 11 || this.data.mobile.startsWith("1") == false) {
  179. wx.showToast({
  180. title: '请输入正确手机号',
  181. icon:'none',
  182. duration: 3000
  183. });
  184. return;
  185. }
  186. util.request(api.AuthCaptcha, {
  187. mobile: this.data.mobile
  188. }, "POST").then(res => {
  189. if (res.errno == 0)
  190. wx.showToast({
  191. title: '验证码发送成功',
  192. duration: 3000
  193. });
  194. else
  195. wx.showToast({
  196. title: res.errmsg,
  197. duration: 3000
  198. });
  199. }).catch(res => {
  200. wx.showToast({
  201. title: res.errMsg,
  202. icon: 'error',
  203. duration: 3000
  204. });
  205. })
  206. },
  207. bindMobileInput(e) {
  208. console.info(e)
  209. this.setData({
  210. mobile: e.detail.value
  211. });
  212. },
  213. bindcaptchaInput(e) {
  214. this.setData({
  215. captcha: e.detail.value
  216. });
  217. },
  218. goInvite(){
  219. wx.navigateTo({
  220. url: '/pages/upgrade/invite/invite',
  221. })
  222. }
  223. })