DiyfieldsController.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. <?php
  2. namespace app\admin\controller;
  3. use think\exception\ValidateException;
  4. use think\facade\Validate;
  5. use app\model\Technical;
  6. use app\model\Agent;
  7. use app\model\Partner;
  8. use app\model\Tuanzhang;
  9. use app\model\Member;
  10. use app\model\Store;
  11. use app\model\Operatingcity;
  12. use app\model\RegisterField;
  13. use app\model\Category;
  14. use app\model\StoreCate;
  15. use app\model\Users;
  16. use app\model\Openid;
  17. class DiyfieldsController extends Base
  18. {
  19. function getModel()
  20. {
  21. $ptype = $this->getPath()[1];
  22. if ($ptype == 'technical') {
  23. return new Technical;
  24. } else if ($ptype == 'agent') {
  25. return new Agent;
  26. } else if ($ptype == 'member') {
  27. return new Member;
  28. } else if ($ptype == 'operatingcity') {
  29. return new Operatingcity;
  30. } else if ($ptype == 'store') {
  31. return new Store;
  32. } else if ($ptype == 'partner') {
  33. return new Partner;
  34. } else if ($ptype == 'tuanzhang') {
  35. return new Tuanzhang;
  36. }
  37. }
  38. function getPath()
  39. {
  40. $path = input('post.path', '', 'serach_in');
  41. if (!empty($path)) {
  42. $tmppath = explode('/', $path);
  43. }
  44. if (empty($tmppath)) {
  45. $con = input('con');
  46. if (!empty($con)) {
  47. $tmppath[1] = $con;
  48. }
  49. }
  50. return $tmppath;
  51. }
  52. public function update()
  53. {
  54. $id = $this->request->post('id');
  55. $uuid = $this->request->post('uuid');
  56. $postdata = only('id,fields');
  57. $regdata = RegisterField::fieldToData($postdata, 'pc');
  58. $data = $regdata['data'];
  59. unset($data['create_time']);
  60. if ($data['username'] && empty($id)) {
  61. $data['uuid'] = uniqid(rand(1, 10000));
  62. }
  63. $usersdata['password'] = $data['password'];
  64. $data['password'] = '12345678';
  65. $validate = Validate::rule($regdata['rule']);
  66. if (!$validate->check($data)) {
  67. throw new ValidateException($validate->getError());
  68. }
  69. unset($data['password']);
  70. if (empty($id)) {
  71. $usersdata['username'] = trim($data['username']);
  72. }
  73. unset($data['username']);
  74. if ($usersdata['username']) {
  75. if (Users::where('username', $usersdata['username'])->find()) {
  76. throw new ValidateException('用户名已被占用');
  77. }
  78. }
  79. if ($this->getPath()[1] == 'technical') {
  80. if ($data['photoalbum']) {
  81. $data['touxiang'] = explode(',', $data['photoalbum'])[0];
  82. }
  83. }
  84. if (empty($id)) {
  85. $data['uid'] = 0;
  86. $data['weid'] = weid();
  87. $data['sort'] = 100;
  88. try {
  89. $res = $this->getModel()->create($data);
  90. if ($this->getPath()[1] == 'store' || $this->getPath()[1] == 'technical' || $this->getPath()[1] == 'tuanzhang' || $this->getPath()[1] == 'operatingcity') {
  91. if ($res->id && !empty($usersdata['username'])) {
  92. if (!empty($usersdata['password'])) {
  93. $usersdata["salt"] = substr(md5(uniqid()), 8, 8);
  94. $usersdata['password'] = pass_hash($usersdata['password'], $usersdata["salt"]);
  95. } else {
  96. unset($usersdata['password']);
  97. }
  98. if ($this->getPath()[1] == 'store') {
  99. $usersdata['sid'] = $res->id;
  100. }
  101. if ($this->getPath()[1] == 'operatingcity') {
  102. $usersdata['ocid'] = $res->id;
  103. }
  104. if ($this->getPath()[1] == 'tuanzhang') {
  105. $usersdata['tzid'] = $res->id;
  106. }
  107. $usersdata['weid'] = weid();
  108. $usersdata['uuid'] = $data['uuid'];
  109. $usersdata['w7uid'] = 0;
  110. Users::create($usersdata);
  111. }
  112. }
  113. } catch (\Exception $e) {
  114. throw new ValidateException($e->getMessage());
  115. }
  116. return $this->json(['msg' => '添加成功', 'data' => $res->id]);
  117. } else {
  118. $data['id'] = $id;
  119. if ($this->getPath()[1] == 'technical') {
  120. $Technicalwhere['id'] = $data['id'];
  121. $Technicalwhere['province_name'] = $data['province_name'];
  122. $Technicalwhere['city_name'] = $data['city_name'];
  123. $Technicalwhere['district_name'] = $data['district_name'];
  124. $Technicalwhere['house_number'] = $data['house_number'];
  125. if (Technical::where($Technicalwhere)->find()) {
  126. unset($data['latitude']);
  127. unset($data['longitude']);
  128. unset($data['region_name']);
  129. }
  130. }
  131. if ($this->getPath()[1] == 'store') {
  132. $storewhere['id'] = $data['id'];
  133. $storewhere['province_name'] = $data['province_name'];
  134. $storewhere['city_name'] = $data['city_name'];
  135. $storewhere['district_name'] = $data['district_name'];
  136. $storewhere['house_number'] = $data['house_number'];
  137. if (Store::where($storewhere)->find()) {
  138. unset($data['latitude']);
  139. unset($data['longitude']);
  140. unset($data['region_name']);
  141. }
  142. }
  143. try {
  144. if (!empty($usersdata['password'])) {
  145. $usersdata["salt"] = substr(md5(uniqid()), 8, 8);
  146. $usersdata['password'] = pass_hash($usersdata['password'], $usersdata["salt"]);
  147. Users::where('uuid', $uuid)->update($usersdata);
  148. } else {
  149. unset($usersdata['password']);
  150. }
  151. $this->getModel()->update($data);
  152. } catch (\Exception $e) {
  153. throw new ValidateException($e->getMessage());
  154. }
  155. return $this->json(['msg' => '操作成功']);
  156. }
  157. }
  158. public function audit()
  159. {
  160. $id = $this->request->post('id');
  161. $data['id'] = $id;
  162. unset($data['create_time']);
  163. $data['status'] = 1;
  164. try {
  165. $this->getModel()->update($data);
  166. } catch (\Exception $e) {
  167. throw new ValidateException($e->getMessage());
  168. }
  169. return $this->json(['msg' => '审核成功']);
  170. }
  171. function getInfo()
  172. {
  173. $id = $this->request->post('id', '', 'serach_in');
  174. $ptype = $this->getPath()[1];
  175. $data = RegisterField::getinputField($ptype);
  176. if (!empty($id)) {
  177. $infodata = $this->getModel()->find($id);
  178. if (!empty($infodata)) {
  179. $infodata = $infodata->toArray();
  180. $infodata['password'] = "";
  181. if (!empty($infodata['uuid'])) {
  182. $infodata['user'] = Users::getuserbyuuid($infodata['uuid']);
  183. unset($infodata['user']['password']);
  184. unset($infodata['user']['salt']);
  185. $infodata['username'] = $infodata['user']['username'];
  186. if (!empty($infodata['user']['id'])) {
  187. $infodata['Openid'] = Openid::getMpOpenidbyuid($infodata['user']['id']);
  188. }
  189. if (empty($infodata['Openid'])) {
  190. $infodata['Openid'] = Openid::getMpOpenidbyuuid($infodata['uuid']);
  191. }
  192. }
  193. $area[0] = areaconversion($infodata['province_name']);
  194. $area[1] = $infodata['city_name'];
  195. if (!empty($infodata['district_name'])) {
  196. $area[2] = $infodata['district_name'];
  197. }
  198. $infodata['region_name'] = $area;
  199. $customtext = iunserializer($infodata['customtext']);
  200. }
  201. }
  202. foreach ($data as &$vo) {
  203. if ($vo['is_sys'] == 1) {
  204. if(!empty($infodata[$vo['fieldsmingcheng']])){
  205. $vo['fieldsvalue'] = $infodata[$vo['fieldsmingcheng']];
  206. }
  207. if ($vo['fieldsmingcheng'] == "sex") {
  208. $vo['selectvaluearray'] = sexarray();
  209. }
  210. if ($vo['fieldsmingcheng'] == 'category_id') {
  211. $vo['selectvaluearray'] = \app\model\TechnicalCategory::getpcarray();
  212. }
  213. if ($vo['fieldsmingcheng'] == 'sid') {
  214. $vo['selectvaluearray'] = Store::getpcarray();
  215. }
  216. if ($vo['fieldsmingcheng'] == 'level') {
  217. if ($vo['ptype'] == 'technical') {
  218. $vo['selectvaluearray'] = \app\model\TechnicalLevel::getpcarray();
  219. } elseif ($vo['ptype'] == 'tuanzhang') {
  220. $vo['selectvaluearray'] = \app\model\TuanzhangLevel::getpcarray();
  221. } elseif ($vo['ptype'] == 'partner') {
  222. $vo['selectvaluearray'] = \app\model\PartnerLevel::getpcarray();
  223. } elseif ($vo['ptype'] == 'operatingcity') {
  224. $vo['selectvaluearray'] = \app\model\OperatingcityLevel::getpcarray();
  225. }
  226. }
  227. if ($vo['fieldsmingcheng'] == 'agent_level') {
  228. if ($vo['ptype'] == 'agent') {
  229. $vo['selectvaluearray'] = \app\model\AgentLevel::getpcarray();
  230. }
  231. }
  232. if ($vo['fieldsmingcheng'] == 'areatype') {
  233. $vo['selectvaluearray'] = \app\model\OperatingcityType::getpcarray();
  234. }
  235. if ($vo['fieldsmingcheng'] == 'stid') {
  236. $vo['selectvaluearray'] = StoreCate::getpcarray();
  237. }
  238. if ($vo['fieldsmingcheng'] == 'gid') {
  239. $vo['selectvaluearray'] = \app\model\MemberAuthGroup::getpcarray();
  240. }
  241. if ($vo['inputtype'] == 'checkbox' && $vo['fieldsmingcheng'] == 'cate_ids') {
  242. if (empty($vo['fieldsvalue'])) {
  243. $vo['fieldsvalue'] = [];
  244. } else {
  245. $vo['fieldsvalue'] = explode(',', $vo['fieldsvalue']);
  246. }
  247. if ($vo['ptype'] == 'technical') {
  248. $ctopptype = 2;
  249. } else {
  250. $ctopptype = '';
  251. }
  252. $vo['selectvaluearray'] = Category::gettoparray($ctopptype);
  253. }
  254. } else {
  255. $vo['fieldsvalue'] = $customtext[$vo['inputtype']][$vo['id']];
  256. if (!empty($vo['selectvalue'])) {
  257. $selectvaluearray = explode(',', $vo['selectvalue']);
  258. if (is_array($selectvaluearray)) {
  259. foreach ($selectvaluearray as $key => $svo) {
  260. $vo['selectvaluearray'][$key]['val'] = $svo;
  261. $vo['selectvaluearray'][$key]['key'] = $svo;
  262. }
  263. }
  264. }
  265. }
  266. if ($vo['inputtype'] == 'pics') {
  267. $vo['fieldsvalue1'] = $vo['fieldsvalue'];
  268. if (empty($vo['fieldsvalue'])) {
  269. $vo['fieldsvalue'] = [];
  270. } else {
  271. $vo['fieldsvalue'] = setPicsView($vo['fieldsvalue']);
  272. }
  273. }
  274. if ($vo['inputtype'] == 'date') {
  275. $vo['fieldsvalue'] = time_format($vo['fieldsvalue']);
  276. }
  277. }
  278. if(!empty($infodata)){
  279. $retdata['id'] = $infodata['id'];
  280. $retdata['uuid'] = $infodata['uuid'];
  281. $retdata['infodata'] = $infodata;
  282. }
  283. $retdata['fields'] = $data;
  284. return $this->json(['data' => $retdata]);
  285. }
  286. function delete()
  287. {
  288. return $this->del($this->getModel());
  289. }
  290. /*
  291. * 导入
  292. */
  293. public function importData()
  294. {
  295. $data = input('post.');
  296. $ptype = $this->getPath()[1];
  297. $mob = $this->getModel();
  298. RegisterField::importData($mob, $ptype, $data);
  299. return $this->json(['msg' => '操作成功']);
  300. }
  301. public function get_idbytitle($list, $title)
  302. {
  303. foreach ($list as $v) {
  304. if ($v['key'] == $title) {
  305. return $v['val'];
  306. }
  307. }
  308. return 0;
  309. }
  310. public function get_idbyarea_name($list, $area_name)
  311. {
  312. foreach ($list as $v) {
  313. if ($v['area_name'] == $area_name) {
  314. return $v['area_id'];
  315. }
  316. }
  317. return 0;
  318. }
  319. }