user.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. // pages/user/user.js
  2. const app = getApp()
  3. var host = app.globalData.host;
  4. const $api = require('../../utils/api.js').API;
  5. var logindata={}
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. usercode: '',
  12. phone:'',
  13. phcode:'',
  14. openid: '',
  15. info: {},
  16. userinfo:{},
  17. todayinfo:{},
  18. todayMoney:'',
  19. status:0,
  20. text:'获取验证码'
  21. },
  22. /**
  23. * 生命周期函数--监听页面加载
  24. */
  25. onLoad: function (options) {
  26. if(options.type){
  27. this.setData({
  28. status:options.type
  29. })
  30. }
  31. },
  32. zx(){
  33. wx.navigateTo({
  34. url: '../message/message',
  35. })
  36. },
  37. getcode(){
  38. if(this.data.phone.length>=11){
  39. $api.sendcode({phone:this.data.phone}).then(res=>{
  40. this.countdown()
  41. })
  42. }else{
  43. wx.showToast({
  44. icon: 'none',
  45. title: '请输入正确的手机号',
  46. })
  47. }
  48. },
  49. countdown(){
  50. var count=60,_this=this;
  51. var timer=setInterval(c,1000)
  52. function c(){
  53. if(count>0){
  54. count--;
  55. _this.setData({
  56. text:count+"s重新发送"
  57. })
  58. }else{
  59. _this.setData({
  60. text:'获取验证码'
  61. })
  62. clearInterval(timer)
  63. }
  64. }
  65. },
  66. getUserData() {
  67. $api.getAuthinfo().then(res=>{
  68. wx.setNavigationBarTitle({
  69. title: '',
  70. })
  71. if(res.data.data.phone == null || res.data.data.phone==''){
  72. return
  73. }
  74. if(res.data.data.role==0){
  75. this.setData({
  76. status:3
  77. })
  78. }else{
  79. this.setData({
  80. usercode: res.data.data.usercode,
  81. userinfo: res.data.data,
  82. status:3
  83. })
  84. }
  85. })
  86. .catch(err=>{
  87. this.setData({
  88. userinfo: {},
  89. status:0
  90. })
  91. })
  92. },
  93. inputchange(e) {
  94. this.setData({
  95. phone: e.detail.value
  96. })
  97. },
  98. codechange(e) {
  99. this.setData({
  100. phcode: e.detail.value
  101. })
  102. },
  103. logout() {
  104. wx.removeStorage({
  105. key: 'userInfo'
  106. })
  107. wx.setNavigationBarTitle({
  108. title: '登录',
  109. })
  110. this.setData({
  111. userinfo: {},
  112. usercode: ''
  113. })
  114. },
  115. /**登陆 */
  116. login: function () {
  117. var _this=this;
  118. // if (!this.data.usercode) {
  119. // wx.showToast({
  120. // icon: 'none',
  121. // title: '请输入用户代码',
  122. // })
  123. // return;
  124. // }
  125. wx.getUserProfile({
  126. desc: '用于完善会员资料',
  127. success: res=> {
  128. //获取基本信息
  129. var data = {
  130. nickName: res.userInfo.nickName,
  131. avatarUrl: res.userInfo.avatarUrl,
  132. usercode: this.data.usercode
  133. }
  134. wx.showLoading({
  135. title: '正在登陆',
  136. })
  137. wx.login({
  138. success(res) {
  139. //获取code
  140. $api.getOpenid({ code: res.code})
  141. .then(res => {
  142. //获取openid
  143. if (res.data.code != 0) {
  144. wx.showToast({
  145. icon: 'none',
  146. title: res.data.message,
  147. })
  148. wx.hideLoading();
  149. return;
  150. }
  151. data.openid = res.data.data.openid;
  152. logindata=data
  153. $api.login(data)
  154. .then(res=>{
  155. //登录成功
  156. //老用户直接进入系统
  157. if(res.data.data.role != 0){
  158. wx.switchTab({
  159. url: '../index/index',
  160. })
  161. }
  162. //新用户
  163. _this.setData({
  164. status:res.data.data.status
  165. })
  166. wx.hideLoading();
  167. wx.setNavigationBarTitle({
  168. title: '',
  169. })
  170. if (res.data.code != 0) {
  171. wx.showToast({
  172. icon: 'none',
  173. title: res.data.message,
  174. })
  175. return;
  176. }
  177. wx.setStorage({
  178. key: 'userInfo',
  179. data: res.data.data,
  180. })
  181. })
  182. .catch(err=>{
  183. wx.hideLoading();
  184. })
  185. })
  186. .catch(err => {
  187. //请求失败
  188. wx.hideLoading();
  189. })
  190. }
  191. })
  192. }
  193. })
  194. },
  195. login1(){
  196. logindata.phone=this.data.phone
  197. logindata.phcode=this.data.phcode
  198. if (logindata.phone.length<11){
  199. wx.showToast({
  200. icon: 'none',
  201. title: '请输入正确的手机号',
  202. })
  203. return
  204. }
  205. if (!logindata.phcode) {
  206. wx.showToast({
  207. icon: 'none',
  208. title: '请输入验证码',
  209. })
  210. return
  211. }
  212. $api.login(logindata).then(res=>{
  213. if(res.data.code == 0){
  214. this.setData({
  215. status:3
  216. })
  217. }
  218. })
  219. },
  220. login2(){
  221. if (this.data.phone.length<11) {
  222. wx.showToast({
  223. icon: 'none',
  224. title: '请输入正确的手机号',
  225. })
  226. return
  227. }
  228. if (!this.data.phcode) {
  229. wx.showToast({
  230. icon: 'none',
  231. title: '请输入验证码',
  232. })
  233. return
  234. }
  235. $api.bindphone({phone:this.data.phone,phcode:this.data.phcode}).then(res=>{
  236. if(res.data.code == 0){
  237. this.setData({
  238. status:3
  239. })
  240. }
  241. })
  242. },
  243. onCustomerService(){
  244. wx.openCustomerServiceChat({
  245. extInfo: {url: 'https://work.weixin.qq.com/kfid/kfcc1bd2a8bddad1dfb'},
  246. corpId: 'wwc4de479c81db4706',
  247. success(res) {}
  248. })
  249. },
  250. /**
  251. * 生命周期函数--监听页面初次渲染完成
  252. */
  253. onReady: function () {
  254. },
  255. /**
  256. * 生命周期函数--监听页面显示
  257. */
  258. onShow: function () {
  259. },
  260. /**
  261. * 生命周期函数--监听页面隐藏
  262. */
  263. onHide: function () {
  264. },
  265. /**
  266. * 生命周期函数--监听页面卸载
  267. */
  268. onUnload: function () {
  269. },
  270. /**
  271. * 页面相关事件处理函数--监听用户下拉动作
  272. */
  273. onPullDownRefresh: function () {
  274. },
  275. /**
  276. * 页面上拉触底事件的处理函数
  277. */
  278. onReachBottom: function () {
  279. },
  280. /**
  281. * 用户点击右上角分享
  282. */
  283. onShareAppMessage: function () {
  284. }
  285. })