DiyfieldsController.php 10 KB

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