index.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. //引入请求
  2. import {
  3. ApiModel
  4. } from "../../utils/api"
  5. //引入校验文件
  6. import {
  7. TestVerify
  8. } from "../../utils/verify"
  9. const _Http = new ApiModel();
  10. const _Verify = new TestVerify();
  11. Page({
  12. /**
  13. * 页面的初始数据
  14. */
  15. data: {
  16. fobsurl: {}, //头像列表
  17. userImg: {}, //使用头像地址
  18. fname: "", //用户名
  19. frole: "", //角色
  20. fphonenumber: "", //手机号
  21. fsex: "", //性别
  22. fbirthdate: "", //生日
  23. femail: "", //邮箱
  24. fwechatno: "", //微信
  25. faddress: "", //地址
  26. /* 必填项未填红色显示 */
  27. inputErrRed: {
  28. isFname: false, //用户名
  29. isFrole: false, //角色
  30. }
  31. },
  32. /**
  33. * 生命周期函数--监听页面加载
  34. */
  35. onLoad: function (options) {
  36. /* 初始化数据 */
  37. const data = JSON.parse(options.data)
  38. this.setData({
  39. fobsurl: data.attinfos,
  40. userImg: data.attinfos[data.attinfos.length - 1],
  41. fname: data.fname,
  42. frole: data.frole,
  43. fsex: data.fsex,
  44. fbirthdate: data.fbirthdate,
  45. femail: data.femail,
  46. fwechatno: data.fwechatno,
  47. faddress: data.faddress,
  48. frole: data.frole,
  49. faddress: data.faddress,
  50. fphonenumber: data.fphonenumber,
  51. })
  52. },
  53. /* 修改用户头像 */
  54. changeUserImg() {
  55. var that = this;
  56. wx.chooseMedia({
  57. count: 1,
  58. mediaType: ['image'],
  59. sourceType: ['album', 'camera'],
  60. camera: 'back',
  61. success(res) {
  62. const file = res.tempFiles[0];
  63. var index = file.tempFilePath.lastIndexOf(".");
  64. var ext = file.tempFilePath.substr(index + 1);
  65. var timestamp = Date.parse(new Date());
  66. wx.getFileSystemManager().readFile({
  67. filePath: file.tempFilePath,
  68. // encoding:'utf-8',
  69. success: result => {
  70. //返回临时文件路径
  71. const fileData = result.data
  72. const userid = getApp().globalData.accountList[wx.getStorageSync('userIndex')].userid
  73. _Http.basic({
  74. "accesstoken": wx.getStorageSync('token'),
  75. "classname": "system.system.docManage",
  76. "method": "getFileName",
  77. "content": {
  78. "filename": 'wx' + timestamp,
  79. "filetype": ext,
  80. "ownertable": "tenterprise_users",
  81. "ownerid": userid,
  82. "ftype": "headportrait",
  83. }
  84. }).then(res => {
  85. /* 上次并查询头像 */
  86. that.uploadFile(res, fileData)
  87. }).catch(err => {
  88. console.log(err)
  89. })
  90. },
  91. fail: console.error
  92. })
  93. }
  94. })
  95. },
  96. /* 上次成功反馈 */
  97. uploadFile(res, data) {
  98. var that = this
  99. wx.request({
  100. url: res.data.obsuploadurl,
  101. method: "PUT",
  102. data: data,
  103. header: {
  104. 'content-type': 'application/octet-stream' // 默认值
  105. },
  106. success() {
  107. _Http.basic({
  108. "accesstoken": wx.getStorageSync('token'),
  109. "classname": "system.system.docManage",
  110. "method": "uploadSuccess",
  111. "content": {
  112. "obsfilename": res.data.obsfilename
  113. }
  114. }).then(res => {
  115. //删除之前头像
  116. that.imageDelete()
  117. that.setData({
  118. fobsurl: res.data[0],
  119. userImg: res.data[res.data.length - 1],
  120. })
  121. }).catch(err => {
  122. console.log(err)
  123. })
  124. }
  125. })
  126. },
  127. /* 删除替换头像 */
  128. imageDelete() {
  129. const userid = getApp().globalData.accountList[wx.getStorageSync('userIndex')].userid
  130. for (let i = 0; i < this.data.fobsurl.length; i++) {
  131. _Http.basic({
  132. "accesstoken": wx.getStorageSync('token'),
  133. "classname": "system.system.docManage",
  134. "method": "deleteDoc",
  135. "content": {
  136. "ownertable": "tenterprise_users",
  137. "ownerid": userid,
  138. "tattachmentid": this.data.fobsurl[i].tattachmentid
  139. }
  140. }).then(s => {
  141. console.log(s)
  142. })
  143. }
  144. },
  145. /* input必填项获取焦点 */
  146. inputEssentialFocus(e) {
  147. const {
  148. name
  149. } = e.currentTarget.dataset;
  150. let inputErrRed = this.data.inputErrRed;
  151. inputErrRed[name] = false;
  152. this.setData({
  153. inputErrRed
  154. })
  155. },
  156. /* input必填项失去焦点 */
  157. inputEssentialBlur(e) {
  158. if (e.detail.value != "" && e.detail.value != " ") return;
  159. const {
  160. name
  161. } = e.currentTarget.dataset;
  162. let inputErrRed = this.data.inputErrRed;
  163. inputErrRed[name] = true;
  164. this.setData({
  165. inputErrRed
  166. })
  167. },
  168. /* 校验必填项 */
  169. inputCertain() {
  170. let state = true,
  171. inputErrRed = this.data.inputErrRed;
  172. if (!_Verify.required(this.data.fname)) {
  173. inputErrRed["isFname"] = true;
  174. state = false;
  175. };
  176. if (!_Verify.required(this.data.frole)) {
  177. inputErrRed["isFrole"] = true;
  178. state = false;
  179. };
  180. this.setData({
  181. inputErrRed
  182. });
  183. return state;
  184. },
  185. /* 表单提交 */
  186. formSubmit() {
  187. /* 检测是否还有为空必填项 */
  188. if (!this.inputCertain()) return wx.showToast({
  189. title: '必填项不可为空!',
  190. icon: "none"
  191. });
  192. /* 发送请求 */
  193. _Http.basic({
  194. "accesstoken": wx.getStorageSync('token'),
  195. "classname": "customer.usercenter.usermsg.usermsg",
  196. "method": "update_usermsg",
  197. "content": {
  198. fname: this.data.fname,
  199. frole: this.data.frole,
  200. fsex: this.data.fsex,
  201. fbirthdate: this.data.fbirthdate,
  202. femail: this.data.femail,
  203. fwechatno: this.data.fwechatno,
  204. faddress: this.data.faddress,
  205. }
  206. }).then(s => {
  207. if (s.msg != "成功") return;
  208. wx.showToast({
  209. title: '保存成功',
  210. })
  211. setTimeout(() => {
  212. wx.navigateBack({
  213. delta: 1
  214. })
  215. }, 500)
  216. this.setData({
  217. userMsg: s.data[0]
  218. })
  219. })
  220. },
  221. /**
  222. * 生命周期函数--监听页面初次渲染完成
  223. */
  224. onReady: function () {
  225. },
  226. /**
  227. * 生命周期函数--监听页面显示
  228. */
  229. onShow: function () {
  230. },
  231. /**
  232. * 生命周期函数--监听页面隐藏
  233. */
  234. onHide: function () {
  235. },
  236. /**
  237. * 生命周期函数--监听页面卸载
  238. */
  239. onUnload: function () {
  240. },
  241. /**
  242. * 页面相关事件处理函数--监听用户下拉动作
  243. */
  244. onPullDownRefresh: function () {
  245. },
  246. /**
  247. * 页面上拉触底事件的处理函数
  248. */
  249. onReachBottom: function () {
  250. },
  251. /**
  252. * 用户点击右上角分享
  253. */
  254. onShareAppMessage: function () {
  255. }
  256. })