Manage.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. <?php
  2. namespace Goods\Lib;
  3. use Dever;
  4. class Manage
  5. {
  6. public function getByName_api()
  7. {
  8. $data = Dever::db('goods/info')->getAllByName(array
  9. (
  10. 'name' => Dever::input('term'))
  11. );
  12. if ($data) {
  13. return $data;
  14. }
  15. return array
  16. (
  17. 0 => array('id' => -1, 'value' => '没有找到您搜索的数据', 'label' => '没有找到您搜索的数据'),
  18. );
  19. }
  20. public function getByCate()
  21. {
  22. $cate = Dever::input('cate');
  23. $data = Dever::db('goods/info')->getAllByCate(array('top_category_id' => $cate));
  24. return Dever::load('manage/database')->getAjax($data);
  25. }
  26. public function search_api()
  27. {
  28. $table = 'goods/info';
  29. $keyword = Dever::input('keyword');
  30. $yes = Dever::input('yes');
  31. $where = array();
  32. $cate = Dever::input('cate');
  33. if ($cate) {
  34. $where['cate_id'] = $cate;
  35. }
  36. $price_type = Dever::input('price_type');
  37. if ($price_type) {
  38. $where['price_type'] = $price_type;
  39. }
  40. if ($yes) {
  41. $yes = Dever::db($table)->search(array('ids' => $yes));
  42. }
  43. if (!$keyword) {
  44. $where['limit'] = '0,50';
  45. $data = Dever::db($table)->search($where);
  46. } else {
  47. $where['name'] = $keyword;
  48. $data = Dever::db($table)->search($where);
  49. }
  50. $result = array();
  51. if ($yes) {
  52. foreach ($yes as $k => $v) {
  53. if (isset($data[$k])) {
  54. unset($data[$k]);
  55. }
  56. $yes[$k]['selected'] = 'selected';
  57. }
  58. $data = $yes + $data;
  59. $data = array_merge($data, array());
  60. } else {
  61. $data = array_merge($data, array());
  62. }
  63. if (!$data) {
  64. Dever::alert('暂无数据');
  65. }
  66. return $data;
  67. }
  68. /**
  69. * 更新信息
  70. *
  71. * @return mixed
  72. */
  73. public function infoUpdate($id, $name, $data)
  74. {
  75. # 更新分类id
  76. $category = Dever::param('category', $data);
  77. if ($category) {
  78. if (is_array($category)) {
  79. $category_id = end($category);
  80. $top_category_id = $category[0];
  81. if (isset($category[1])) {
  82. $second_category_id = $category[1];
  83. } else {
  84. $second_category_id = $category[0];
  85. }
  86. } else {
  87. $category_id = $category;
  88. $top_category_id = $category;
  89. $second_category_id = $category;
  90. }
  91. $update['top_category_id'] = $top_category_id;
  92. $update['second_category_id'] = $second_category_id;
  93. $update['category_id'] = $category_id;
  94. }
  95. $pic = Dever::param('pic', $data);
  96. $pic_cover = Dever::param('pic_cover', $data);
  97. if ($pic && !$pic_cover) {
  98. if (is_string($pic)) {
  99. $pic = explode(',', $pic);
  100. }
  101. $update['pic_cover'] = $pic[0];
  102. }
  103. if (isset($update)) {
  104. $update['where_id'] = $id;
  105. Dever::db('goods/info')->update($update);
  106. }
  107. }
  108. /**
  109. * 更新需求属性信息
  110. *
  111. * @return mixed
  112. */
  113. public function attrUpdate($id, $name, $data)
  114. {
  115. print_r($data);die;
  116. foreach ($data as $k => $v) {
  117. }
  118. }
  119. public function column($cate, $im = ',')
  120. {
  121. if ($cate) {
  122. $cate = explode(',', $cate);
  123. $result = array();
  124. foreach ($cate as $k => $v) {
  125. $info = Dever::db('goods/column')->one($v);
  126. $result[$k] = $info['name'];
  127. }
  128. $result = implode($im, $result);
  129. return $result;
  130. }
  131. return '';
  132. }
  133. /**
  134. * 显示信息
  135. *
  136. * @return mixed
  137. */
  138. public function info($id)
  139. {
  140. $info = Dever::db('goods/info')->one($id);
  141. if ($info) {
  142. $table['编号'] = $info['id'];
  143. $table['栏目'] = $this->column($info['column_id']);
  144. //$table['标题'] = $info['name'];
  145. $table['分类'] = Dever::load("category/api.string", $info['category']);
  146. if ($info['cover']) {
  147. $table['商品图'] = '<img src="'.$info['cover'].'" width="150" />';
  148. }
  149. if ($info['price_type'] == 3) {
  150. # 获取套餐的商品
  151. $goods = Dever::array_decode($info['goods']);
  152. foreach ($goods as $k => $v) {
  153. $goods_info = Dever::db('goods/info')->one($v['goods_id']);
  154. if ($goods_info) {
  155. $table['套餐商品'][] = $v['num'] . '个' . $goods_info['name'];
  156. }
  157. }
  158. $table['套餐商品'] = implode(',', $table['套餐商品']);
  159. }
  160. $attr = $this->attrInfo($info);
  161. if ($attr) {
  162. $table['属性'] = $attr;
  163. }
  164. $table['价格'] = Dever::load('goods/lib/sku')->table($info['id'], false, false, false, false);
  165. if ($info['udate']) {
  166. $table['更新时间'] = date("Y-m-d H:i:s", $info['udate']);
  167. }
  168. if ($info['cdate']) {
  169. $table['发布时间'] = date("Y-m-d H:i:s", $info['cdate']);
  170. }
  171. }
  172. return Dever::table($table);
  173. }
  174. private function attrInfo($info)
  175. {
  176. $table = array();
  177. $class = Info::init($info['top_category_id']);
  178. if ($info) {
  179. $info = $class->info($info);
  180. if ($info && isset($info['attr'])) {
  181. foreach ($info['attr'] as $k => $v) {
  182. $table[$v['name']] = $v['value_string'];
  183. }
  184. }
  185. }
  186. return $table ? Dever::table($table) : false;
  187. }
  188. public function area($id, $name, $data)
  189. {
  190. # 不再执行syncone等后续操作
  191. Dever::config('base')->after = 1;
  192. $type = Dever::input('type');
  193. $area = Dever::param('area', $data);
  194. if ($area) {
  195. if (isset($area[2])) {
  196. $update['district_id'] = $area[2];
  197. }
  198. if (isset($area[1])) {
  199. $update['city_id'] = $area[1];
  200. }
  201. if (isset($area[0])) {
  202. $update['province_id'] = $area[0];
  203. }
  204. if (isset($update)) {
  205. $update['where_id'] = $id;
  206. Dever::db($type . '/info')->update($update);
  207. }
  208. }
  209. }
  210. /**
  211. * 显示用户信息
  212. *
  213. * @return mixed
  214. */
  215. public function showUserInfo($id)
  216. {
  217. $info = Dever::db('goods/card_code')->find($id);
  218. $table = array();
  219. if ($info && $info['status'] > 1 && $info['uid'] && $info['uid'] > 0) {
  220. $user = Dever::load('passport/user-one', $info['uid']);
  221. $shop = Dever::load('shop/info-one', $info['shop_id']);
  222. if ($user) {
  223. $table['领取门店'] = $shop['name'] . '('.$shop['mobile'].')';
  224. $table['领取人'] = $user['username'] . '('.$info['uid'].')';
  225. $table['手机号'] = $user['mobile'];
  226. $table['兑换时间'] = date('Y-m-d H:i:s', $info['ddate']);
  227. }
  228. }
  229. return Dever::table($table);
  230. }
  231. /**
  232. * 创建兑换码
  233. *
  234. * @return mixed
  235. */
  236. public function createCard($id, $name, $param)
  237. {
  238. $num = Dever::param('num', $param);
  239. $card_id = Dever::param('card_id', $param);
  240. $info = Dever::db('goods/card')->find($card_id);
  241. if ($info && $num > 0) {
  242. for ($i = 0; $i< $num;$i++) {
  243. $this->createCode($info);
  244. }
  245. }
  246. }
  247. private function createCode($info)
  248. {
  249. # 生成卡号和密码
  250. $card = $info['card_prefix'] . Dever::rand($info['card_len'], $info['card_type'] - 1);
  251. $pwd = $info['pwd_prefix'] . Dever::rand($info['pwd_len'], $info['pwd_type'] - 1);
  252. $where['card'] = $card;
  253. $where['pwd'] = $pwd;
  254. $state = Dever::db('goods/card_code')->find($where);
  255. if (!$state) {
  256. $where['card_id'] = $info['id'];
  257. if ($info['type'] == 1) {
  258. $info['day'] = 3650;
  259. }
  260. $where['edate'] = time() + ($info['day']*86400);
  261. Dever::db('goods/card_code')->insert($where);
  262. } else {
  263. $this->createCode($info);
  264. }
  265. }
  266. /**
  267. * 作废
  268. *
  269. * @return mixed
  270. */
  271. public function drop_api($id)
  272. {
  273. $update['where_id'] = $id;
  274. $update['type'] = 4;
  275. Dever::db('goods/card_code')->update($update);
  276. return 'ok';
  277. }
  278. public function recovery_api($id)
  279. {
  280. $update['where_id'] = $id;
  281. $update['type'] = 1;
  282. Dever::db('goods/card_code')->update($update);
  283. return 'ok';
  284. }
  285. }