index.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. // pages/create-project-role/index.js
  2. import http from '../../utils/http'
  3. import util from '../../utils/util'
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. keyword: '',
  10. projects: [],
  11. projectIndex: -1,
  12. users: [],
  13. userIndex: -1,
  14. roleIndex: -1,
  15. id: -1,
  16. // create/edit
  17. type: 'create',
  18. project_user: null,
  19. roles: []
  20. },
  21. /**
  22. * 生命周期函数--监听页面加载
  23. */
  24. onLoad: function (options) {
  25. this.getProjects()
  26. if (options.id) {
  27. this.setData({
  28. id: options.id,
  29. type: 'edit'
  30. })
  31. this.getProjectUser()
  32. wx.setNavigationBarTitle({
  33. title: '修改权限'
  34. })
  35. }
  36. this.getRoles()
  37. },
  38. updateIndex: function () {
  39. var project_user = this.data.project_user
  40. if (!project_user) return false;
  41. var projects = this.data.projects
  42. if (projects.length > 0) {
  43. for (var i = 0; i < projects.length; ++i) {
  44. if (projects[i].id == project_user.project_id) {
  45. this.setData({
  46. projectIndex: i
  47. })
  48. break;
  49. }
  50. }
  51. }
  52. var roles = this.data.roles
  53. if (roles.length > 0) {
  54. for (var i = 0; i < roles.length; ++i) {
  55. if (roles[i].id == project_user.project_role_id) {
  56. this.setData({
  57. roleIndex: i
  58. })
  59. break;
  60. }
  61. }
  62. }
  63. },
  64. getProjectUser() {
  65. var that = this
  66. http({
  67. url: 'project-users/detail',
  68. data: {
  69. id: this.data.id
  70. },
  71. success: function (res) {
  72. if (res.code == 0) {
  73. that.setData({
  74. project_user: res.data
  75. })
  76. that.updateIndex()
  77. that.search()
  78. }
  79. }
  80. })
  81. },
  82. submit() {
  83. if (this.data.users.length <= 0) {
  84. util.error('请选择成员')
  85. return false
  86. }
  87. if (this.data.projectIndex < 0) {
  88. util.error('请选择项目')
  89. return false
  90. }
  91. if (this.data.roleIndex < 0) {
  92. util.error('请选择角色')
  93. return false
  94. }
  95. var user_id = this.data.users[0].id;
  96. var project_id = this.data.projects[this.data.projectIndex].id;
  97. var role_id = this.data.roles[this.data.roleIndex].id;
  98. var url = this.data.type == 'create' ? 'projects/addUser' : 'projects/updateUser'
  99. var data = {
  100. user_id: user_id,
  101. project_id: project_id,
  102. project_role_id: role_id
  103. }
  104. if (this.data.type == 'edit') {
  105. data.id = this.data.project_user.id
  106. }
  107. http({
  108. url: url,
  109. data: data,
  110. success: function (res) {
  111. if (res.code == 0) {
  112. util.success('操作成功')
  113. setTimeout(() => {
  114. wx.navigateBack({
  115. delta: 1,
  116. })
  117. }, 1000);
  118. }
  119. }
  120. })
  121. },
  122. clear: function () {
  123. this.setData({
  124. keyword: ''
  125. })
  126. },
  127. blur: function () {
  128. var that = this
  129. setTimeout(function () {
  130. that.search()
  131. }, 300)
  132. },
  133. search: function () {
  134. var that = this
  135. if (!this.data.keyword && this.data.type == 'create') return false;
  136. var data = {
  137. keyword: this.data.keyword
  138. }
  139. if (this.data.type == 'edit') {
  140. data = {
  141. keyword: this.data.keyword,
  142. id: this.data.project_user.user_id
  143. }
  144. console.log(data)
  145. }
  146. http({
  147. url: 'users/search',
  148. data: data,
  149. success: function (res) {
  150. if (res.code == 0) {
  151. if (res.data.length <= 0) {
  152. util.error('未找到相应用户')
  153. }
  154. that.setData({
  155. users: res.data
  156. })
  157. that.getProjects()
  158. }
  159. }
  160. })
  161. },
  162. bindPickerChange: function (e) {
  163. var name = e.currentTarget.dataset.name
  164. this.setData({
  165. [name]: e.detail.value
  166. })
  167. },
  168. getRoles: function () {
  169. var that = this
  170. http({
  171. url: 'project-roles/getByExclude',
  172. data: {
  173. id: this.data.id
  174. },
  175. success: function (res) {
  176. if (res.code == 0) {
  177. that.setData({
  178. roles: res.data
  179. })
  180. if (that.data.type == 'edit') that.updateIndex()
  181. }
  182. }
  183. })
  184. },
  185. getProjects: function () {
  186. var that = this
  187. var users = this.data.users
  188. var user_id = ''
  189. if (users.length > 0) {
  190. user_id = users[0].id
  191. }
  192. http({
  193. url: 'projects/getAll',
  194. data: {
  195. user_id: user_id,
  196. self: true,
  197. type: that.data.type
  198. },
  199. success: function (res) {
  200. if (res.code == 0) {
  201. that.setData({
  202. projects: res.data,
  203. projectIndex: -1
  204. })
  205. if (that.data.type == 'edit') that.updateIndex()
  206. }
  207. }
  208. })
  209. },
  210. updateInput: function (e) {
  211. var name = e.currentTarget.dataset.name
  212. this.setData({
  213. [name]: e.detail.value
  214. })
  215. },
  216. /**
  217. * 生命周期函数--监听页面初次渲染完成
  218. */
  219. onReady: function () {
  220. },
  221. /**
  222. * 生命周期函数--监听页面显示
  223. */
  224. onShow: function () {
  225. },
  226. /**
  227. * 生命周期函数--监听页面隐藏
  228. */
  229. onHide: function () {
  230. },
  231. /**
  232. * 生命周期函数--监听页面卸载
  233. */
  234. onUnload: function () {
  235. },
  236. /**
  237. * 页面相关事件处理函数--监听用户下拉动作
  238. */
  239. onPullDownRefresh: function () {
  240. },
  241. /**
  242. * 页面上拉触底事件的处理函数
  243. */
  244. onReachBottom: function () {
  245. },
  246. /**
  247. * 用户点击右上角分享
  248. */
  249. onShareAppMessage: function () {
  250. }
  251. })