uinfo.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. import { $wuxToast } from '../../dist/index'
  2. import { $wuxSelect } from '../../dist/index';
  3. const app = getApp()
  4. Page({
  5. data: {
  6. motto: 'Hello World',
  7. userInfo: {},
  8. hasUserInfo: false,
  9. canIUse: wx.canIUse('button.open-type.getUserInfo'),
  10. openid: '',
  11. message: '',
  12. tel: '',
  13. name: '',
  14. upwd: '',
  15. id:'',
  16. utype:'',
  17. title2: '',
  18. value2: '',
  19. dlist: [],
  20. cids:[]
  21. },
  22. //事件处理函数
  23. bindViewTap: function() {
  24. wx.navigateTo({
  25. url: '../logs/logs'
  26. })
  27. },
  28. onLoad: function () {
  29. var that = this
  30. console.log("孙");
  31. console.log("-"+app.globalData.quanju);
  32. if (app.globalData.userInfo) {
  33. this.setData({
  34. userInfo: app.globalData.userInfo,
  35. hasUserInfo: true
  36. })
  37. } else if (this.data.canIUse){
  38. // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
  39. // 所以此处加入 callback 以防止这种情况
  40. app.userInfoReadyCallback = res => {
  41. this.setData({
  42. userInfo: res.userInfo,
  43. hasUserInfo: true
  44. })
  45. }
  46. } else {
  47. // 在没有 open-type=getUserInfo 版本的兼容处理
  48. wx.getUserInfo({
  49. success: res => {
  50. app.globalData.userInfo = res.userInfo
  51. this.setData({
  52. userInfo: res.userInfo,
  53. hasUserInfo: true
  54. })
  55. }
  56. })
  57. }
  58. // 获取openid
  59. wx.login({
  60. success: function (res) {
  61. if (res.code) {
  62. wx.getUserInfo({
  63. success: function (res) {
  64. console.log("存在code")
  65. }
  66. });
  67. var appid = "wxc55158faad3e366a"
  68. var secret = "f38f654411e153673ff4a473f1ccefd0"
  69. var openid = ""
  70. var l = 'https://api.weixin.qq.com/sns/jscode2session?appid=' + appid + '&secret=' + secret + '&js_code=' + res.code + '&grant_type=authorization_code';
  71. wx.request({
  72. url: l,
  73. data: {},
  74. method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  75. // header: {}, // 设置请求的 header
  76. success: function (res) {
  77. var obj = {};
  78. obj.openid = res.data.openid;
  79. console.log("取得的openid==" + res.data.openid);
  80. that.setData({
  81. openid: obj.openid
  82. })
  83. obj.expires_in = Date.now() + res.data.expires_in;
  84. // console.log(obj);
  85. // wx.setStorageSync('user', obj);//存储openid
  86. }
  87. });
  88. } else {
  89. console.log('获取用户登录态失败!' + res.errMsg)
  90. }
  91. }
  92. });
  93. //获取登录用户的code
  94. // wx.login({
  95. // success(res){
  96. // if(res.code){
  97. // console.log("当前code存在")
  98. // //发送code
  99. // wx.request({
  100. // url: 'http://localhost:8080/sendOpenid',
  101. // data:{
  102. // code:res.code
  103. // }
  104. // })
  105. // }
  106. // else{
  107. // console.log('登录失败!' + res.errMsg)
  108. // }
  109. // }
  110. // })
  111. },
  112. getUserInfo: function(e) {
  113. console.log(e)
  114. console.log("---");
  115. app.globalData.userInfo = e.detail.userInfo
  116. this.setData({
  117. userInfo: e.detail.userInfo,
  118. hasUserInfo: true
  119. })
  120. },
  121. onChange(e) {
  122. let that = this;
  123. let id = e.currentTarget.id;
  124. let values = e.detail.value;
  125. if (id == 'tel') {
  126. that.setData({
  127. tel: values
  128. });
  129. }
  130. if (id == 'name') {
  131. that.setData({
  132. name: values
  133. });
  134. }
  135. if (id == 'upwd') {
  136. that.setData({
  137. upwd: values
  138. });
  139. }
  140. },
  141. saveUser: function () {
  142. let tel = this.data.tel;
  143. if (tel == null || tel == '') {
  144. this.showError('请输入电话');
  145. return;
  146. }
  147. let upwd = this.data.upwd;
  148. if (upwd == null || upwd == '') {
  149. this.showError('请输入密码');
  150. return;
  151. }
  152. let name = this.data.name;
  153. if (name == null || name == '') {
  154. this.showError('请输入姓名');
  155. return;
  156. }
  157. let value2 = this.data.value2;
  158. if (value2 == null || value2 == '') {
  159. wx.showModal({
  160. title: '提示',
  161. content: '请选择个人喜好',
  162. showCancel: false
  163. })
  164. return
  165. }
  166. let utype = this.data.utype;
  167. let id = app.globalData.openid;
  168. wx.request({
  169. url: app.data.resturl + '/updateuser.action',
  170. method: 'post',
  171. header: {
  172. 'content-type': 'application/x-www-form-urlencoded'
  173. },
  174. data: {
  175. name: name,
  176. tel: tel,
  177. upwd: upwd,
  178. utype: value2,
  179. id: id
  180. },
  181. success: function (res) {
  182. let code = res.data.code;
  183. if (code == 200) {
  184. wx.navigateBack({
  185. })
  186. }
  187. }
  188. })
  189. },
  190. onClick2() {
  191. console.log(this.data.value2);
  192. $wuxSelect('#wux-select2').open({
  193. multiple: true,
  194. value: this.data.cids,
  195. options: this.data.dlist,
  196. onChange: (value, index, options) => {
  197. console.log('onChange', value, index, options)
  198. this.setData({
  199. value2: value,
  200. title2: index.map((n) => options[n].title),
  201. })
  202. },
  203. onConfirm: (value, index, options) => {
  204. console.log('onConfirm', value, index, options)
  205. this.setData({
  206. value2: value,
  207. title2: index.map((n) => options[n].title),
  208. })
  209. },
  210. })
  211. },
  212. showError: function (info) {
  213. $wuxToast().show({
  214. type: 'text',
  215. duration: 1500,
  216. color: '#fff',
  217. text: info
  218. })
  219. },
  220. onShow: function () {
  221. var that = this;
  222. this.getUserinfo();
  223. this.clist();
  224. },
  225. getUserinfo() {
  226. var that = this;
  227. let openid = app.globalData.openid;
  228. console.log("openid=" + openid);
  229. if (openid == '') {
  230. wx.redirectTo({
  231. url: '../../pages/login/login',
  232. })
  233. }
  234. wx.request({
  235. url: app.data.resturl + '/uinfo.action',
  236. method: 'post',
  237. header: {
  238. 'content-type': 'application/x-www-form-urlencoded'
  239. },
  240. data: {
  241. uid: openid
  242. },
  243. success: function (res) {
  244. let uinfo = res.data.uinfo;
  245. that.setData({
  246. upwd: uinfo.upwd,
  247. name:uinfo.name,
  248. tel:uinfo.tel,
  249. utype: uinfo.utype,
  250. title2: uinfo.cname,
  251. value2: uinfo.utype,
  252. cids:uinfo.cids
  253. });
  254. }
  255. })
  256. },
  257. clist: function () {
  258. let that = this;
  259. wx.request({
  260. url: app.data.resturl + '/cateloglist.action',
  261. data: {
  262. },
  263. success: function (res) {
  264. console.log(res.data.dlist);
  265. that.setData({
  266. dlist: res.data.dlist
  267. })
  268. }
  269. })
  270. }
  271. })