user.js 6.7 KB

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