user.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. const defaultAvatarUrl = '../../images/userdefault.png'
  7. Page({
  8. /**
  9. * 页面的初始数据
  10. */
  11. data: {
  12. usercode: '',
  13. phone:'',
  14. phcode:'',
  15. openid: '',
  16. info: {},
  17. userinfo:{},
  18. todayinfo:{},
  19. todayMoney:'',
  20. status:0,
  21. text:'获取验证码',
  22. player_type:0,
  23. signinfo:{},
  24. avatarUrl: defaultAvatarUrl,
  25. nickName:'',
  26. openid:'',
  27. unionid:'',
  28. },
  29. /**
  30. * 生命周期函数--监听页面加载
  31. */
  32. onLoad: function (options) {
  33. if(options.type){
  34. this.setData({
  35. status:options.type,
  36. player_type:options.player_type
  37. })
  38. }
  39. // $api.getsignup().then(res=>{
  40. // this.setData({
  41. // signinfo:res.data.data
  42. // })
  43. // })
  44. },
  45. onShow: function () {
  46. wx.hideHomeButton();
  47. },
  48. zx(){
  49. wx.navigateTo({
  50. url: '../message/message',
  51. })
  52. },
  53. getcode(){
  54. if(this.data.phone.length>=11){
  55. $api.sendcode({phone:this.data.phone}).then(res=>{
  56. this.countdown()
  57. })
  58. }else{
  59. wx.showToast({
  60. icon: 'none',
  61. title: '请输入正确的手机号',
  62. })
  63. }
  64. },
  65. countdown(){
  66. var count=60,_this=this;
  67. var timer=setInterval(c,1000)
  68. function c(){
  69. if(count>0){
  70. count--;
  71. _this.setData({
  72. text:count+"s重新发送"
  73. })
  74. }else{
  75. _this.setData({
  76. text:'获取验证码'
  77. })
  78. clearInterval(timer)
  79. }
  80. }
  81. },
  82. onChooseAvatar(e) {
  83. const { avatarUrl } = e.detail
  84. this.setData({
  85. avatarUrl,
  86. })
  87. this.uploadImg(this.data.avatarUrl)
  88. },
  89. nameChange(e){
  90. this.setData({
  91. nickName: e.detail.value
  92. })
  93. },
  94. inputchange(e) {
  95. this.setData({
  96. phone: e.detail.value
  97. })
  98. },
  99. codechange(e) {
  100. this.setData({
  101. phcode: e.detail.value
  102. })
  103. },
  104. logout() {
  105. wx.removeStorage({
  106. key: 'userInfo'
  107. })
  108. wx.setNavigationBarTitle({
  109. title: '登录',
  110. })
  111. this.setData({
  112. userinfo: {},
  113. usercode: ''
  114. })
  115. },
  116. /**登陆 */
  117. login: function () {
  118. this.islogin();
  119. },
  120. register(e){
  121. if(this.data.avatarUrl == defaultAvatarUrl){
  122. wx.showToast({
  123. icon: 'none',
  124. title: '请选择头像',
  125. })
  126. return
  127. }
  128. if(this.data.nickName == ''){
  129. wx.showToast({
  130. icon: 'none',
  131. title: '请输入昵称',
  132. })
  133. return
  134. }
  135. this.wxlogin()
  136. },
  137. islogin(){
  138. var _this=this;
  139. wx.showLoading({
  140. title: '正在登陆',
  141. })
  142. wx.login({
  143. success(res) {
  144. //获取code
  145. $api.getOpenid({ code: res.code})
  146. .then(res => {
  147. //获取openid
  148. if (res.data.code != 0) {
  149. wx.showToast({
  150. icon: 'none',
  151. title: res.data.message,
  152. })
  153. wx.hideLoading();
  154. return;
  155. }
  156. _this.setData({
  157. openid:res.data.data.openid,
  158. unionid:res.data.data.unionid,
  159. })
  160. $api.isUserLogin({openid:res.data.data.openid,unionid:res.data.data.unionid})
  161. .then(res=>{
  162. wx.hideLoading();
  163. if (res.data.code != 0) {
  164. wx.showToast({
  165. icon: 'none',
  166. title: res.data.message,
  167. })
  168. return;
  169. }
  170. //有登录
  171. if(res.data.data.id){
  172. _this.setData({
  173. nickName:res.data.data.username,
  174. avatarUrl:res.data.data.avatar,
  175. })
  176. _this.wxlogin()
  177. } else{
  178. _this.setData({
  179. status:10,
  180. })
  181. }
  182. })
  183. .catch(err=>{
  184. wx.hideLoading();
  185. })
  186. })
  187. .catch(err => {
  188. //请求失败
  189. wx.hideLoading();
  190. })
  191. }
  192. })
  193. },
  194. wxlogin(){
  195. var _this=this;
  196. wx.showLoading({
  197. title: '正在登陆',
  198. })
  199. var udata = {};
  200. udata.openid = this.data.openid;
  201. udata.unionid = this.data.unionid;
  202. udata.nickName = this.data.nickName;
  203. udata.avatarUrl = this.data.avatarUrl;
  204. $api.login(udata)
  205. .then(res=>{
  206. //登录成功
  207. wx.hideLoading();
  208. if (res.data.code != 0) {
  209. wx.showToast({
  210. icon: 'none',
  211. title: res.data.message,
  212. })
  213. return;
  214. }
  215. wx.setStorage({
  216. key: 'userInfo',
  217. data: res.data.data,
  218. })
  219. //老用户直接进入系统
  220. if(res.data.data.role != 0){
  221. app.globalData.follow = 1
  222. wx.switchTab({
  223. url: '../index/index',
  224. })
  225. return;
  226. }
  227. //新用户选择报名
  228. _this.setData({
  229. status:3,
  230. player_type:0
  231. })
  232. wx.setNavigationBarTitle({
  233. title: '',
  234. })
  235. })
  236. .catch(err=>{
  237. wx.hideLoading();
  238. })
  239. },
  240. login1(){
  241. logindata.phone=this.data.phone
  242. logindata.phcode=this.data.phcode
  243. if (logindata.phone.length<11){
  244. wx.showToast({
  245. icon: 'none',
  246. title: '请输入正确的手机号',
  247. })
  248. return
  249. }
  250. if (!logindata.phcode) {
  251. wx.showToast({
  252. icon: 'none',
  253. title: '请输入验证码',
  254. })
  255. return
  256. }
  257. $api.login(logindata).then(res=>{
  258. if(res.data.code == 0){
  259. this.setData({
  260. status:3
  261. })
  262. }
  263. })
  264. },
  265. login2(){
  266. if (this.data.phone.length<11) {
  267. wx.showToast({
  268. icon: 'none',
  269. title: '请输入正确的手机号',
  270. })
  271. return
  272. }
  273. if (!this.data.phcode) {
  274. wx.showToast({
  275. icon: 'none',
  276. title: '请输入验证码',
  277. })
  278. return
  279. }
  280. $api.bindphone({phone:this.data.phone,phcode:this.data.phcode}).then(res=>{
  281. if(res.data.code == 0){
  282. this.setData({
  283. status:3
  284. })
  285. }
  286. })
  287. },
  288. uploadImg(path){
  289. $api.uploadAvatar(path,'file').then(res=>{
  290. if(res.code == 0){
  291. this.setData({
  292. avatarUrl: res.data.url
  293. })
  294. }
  295. })
  296. },
  297. onCustomerService(){
  298. wx.openCustomerServiceChat({
  299. extInfo: {url: 'https://work.weixin.qq.com/kfid/kfcc1bd2a8bddad1dfb'},
  300. corpId: 'wwc4de479c81db4706',
  301. success(res) {}
  302. })
  303. },
  304. /**
  305. * 生命周期函数--监听页面初次渲染完成
  306. */
  307. onReady: function () {
  308. },
  309. /**
  310. * 生命周期函数--监听页面显示
  311. */
  312. onShow: function () {
  313. },
  314. /**
  315. * 生命周期函数--监听页面隐藏
  316. */
  317. onHide: function () {
  318. },
  319. /**
  320. * 生命周期函数--监听页面卸载
  321. */
  322. onUnload: function () {
  323. },
  324. /**
  325. * 页面相关事件处理函数--监听用户下拉动作
  326. */
  327. onPullDownRefresh: function () {
  328. },
  329. /**
  330. * 页面上拉触底事件的处理函数
  331. */
  332. onReachBottom: function () {
  333. },
  334. /**
  335. * 用户点击右上角分享
  336. */
  337. onShareAppMessage: function () {
  338. }
  339. })