user.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. var nopenid = res.data.data.openid
  157. var nunionid = res.data.data.unionid
  158. _this.setData({
  159. openid:nopenid,
  160. unionid:nunionid,
  161. })
  162. // console.log(res.data.data.unionid,222)
  163. $api.isUserLogin({openid:nopenid,unionid:nunionid})
  164. .then(res=>{
  165. wx.hideLoading();
  166. if (res.data.code != 0) {
  167. wx.showToast({
  168. icon: 'none',
  169. title: res.data.message,
  170. })
  171. return;
  172. }
  173. //有登录
  174. if(res.data.data.id){
  175. _this.setData({
  176. nickName:res.data.data.username,
  177. avatarUrl:res.data.data.avatar,
  178. })
  179. _this.wxlogin()
  180. } else{
  181. _this.setData({
  182. status:10,
  183. })
  184. }
  185. })
  186. .catch(err=>{
  187. wx.hideLoading();
  188. })
  189. })
  190. .catch(err => {
  191. //请求失败
  192. wx.hideLoading();
  193. })
  194. }
  195. })
  196. },
  197. wxlogin(){
  198. var _this=this;
  199. wx.showLoading({
  200. title: '正在登陆',
  201. })
  202. var udata = {};
  203. udata.openid = this.data.openid;
  204. udata.unionid = this.data.unionid;
  205. udata.nickName = this.data.nickName;
  206. udata.avatarUrl = this.data.avatarUrl;
  207. $api.login(udata)
  208. .then(res=>{
  209. //登录成功
  210. wx.hideLoading();
  211. if (res.data.code != 0) {
  212. wx.showToast({
  213. icon: 'none',
  214. title: res.data.message,
  215. })
  216. return;
  217. }
  218. // console.log(res.data.data,'user')
  219. wx.setStorage({
  220. key: 'userInfo',
  221. data: res.data.data,
  222. })
  223. wx.setStorage({
  224. key: 'user',
  225. data: res.data.data,
  226. })
  227. //老用户直接进入系统
  228. if(res.data.data.role != 0){
  229. app.globalData.follow = 1
  230. wx.switchTab({
  231. url: '../index/index',
  232. })
  233. return;
  234. }
  235. //新用户选择报名
  236. _this.setData({
  237. status:3,
  238. player_type:0
  239. })
  240. wx.setNavigationBarTitle({
  241. title: '',
  242. })
  243. })
  244. .catch(err=>{
  245. wx.hideLoading();
  246. })
  247. },
  248. login1(){
  249. logindata.phone=this.data.phone
  250. logindata.phcode=this.data.phcode
  251. if (logindata.phone.length<11){
  252. wx.showToast({
  253. icon: 'none',
  254. title: '请输入正确的手机号',
  255. })
  256. return
  257. }
  258. if (!logindata.phcode) {
  259. wx.showToast({
  260. icon: 'none',
  261. title: '请输入验证码',
  262. })
  263. return
  264. }
  265. $api.login(logindata).then(res=>{
  266. if(res.data.code == 0){
  267. this.setData({
  268. status:3
  269. })
  270. }
  271. })
  272. },
  273. login2(){
  274. if (this.data.phone.length<11) {
  275. wx.showToast({
  276. icon: 'none',
  277. title: '请输入正确的手机号',
  278. })
  279. return
  280. }
  281. if (!this.data.phcode) {
  282. wx.showToast({
  283. icon: 'none',
  284. title: '请输入验证码',
  285. })
  286. return
  287. }
  288. $api.bindphone({phone:this.data.phone,phcode:this.data.phcode}).then(res=>{
  289. if(res.data.code == 0){
  290. this.setData({
  291. status:3
  292. })
  293. }
  294. })
  295. },
  296. uploadImg(path){
  297. $api.uploadAvatar(path,'file').then(res=>{
  298. if(res.code == 0){
  299. this.setData({
  300. avatarUrl: res.data.url
  301. })
  302. }
  303. })
  304. },
  305. onCustomerService(){
  306. wx.openCustomerServiceChat({
  307. extInfo: {url: 'https://work.weixin.qq.com/kfid/kfcc1bd2a8bddad1dfb'},
  308. corpId: 'wwc4de479c81db4706',
  309. success(res) {}
  310. })
  311. },
  312. /**
  313. * 生命周期函数--监听页面初次渲染完成
  314. */
  315. onReady: function () {
  316. },
  317. /**
  318. * 生命周期函数--监听页面显示
  319. */
  320. onShow: function () {
  321. },
  322. /**
  323. * 生命周期函数--监听页面隐藏
  324. */
  325. onHide: function () {
  326. },
  327. /**
  328. * 生命周期函数--监听页面卸载
  329. */
  330. onUnload: function () {
  331. },
  332. /**
  333. * 页面相关事件处理函数--监听用户下拉动作
  334. */
  335. onPullDownRefresh: function () {
  336. },
  337. /**
  338. * 页面上拉触底事件的处理函数
  339. */
  340. onReachBottom: function () {
  341. },
  342. /**
  343. * 用户点击右上角分享
  344. */
  345. onShareAppMessage: function () {
  346. }
  347. })