Sellervoucher.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. <?php
  2. namespace app\api\controller;
  3. use think\facade\View;
  4. use think\facade\Lang;
  5. use think\facade\Db;
  6. /**
  7. * ============================================================================
  8. *
  9. * ============================================================================
  10. *
  11. * ----------------------------------------------------------------------------
  12. *
  13. * ============================================================================
  14. * 卖家代金券控制器
  15. */
  16. class Sellervoucher extends MobileSeller
  17. {
  18. private $quotastate_arr;
  19. private $templatestate_arr;
  20. public function initialize()
  21. {
  22. parent::initialize(); // TODO: Change the autogenerated stub
  23. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/sellervoucher.lang.php');
  24. if (config('ds_config.voucher_allow') != 1) {
  25. ds_json_encode(10001, lang('voucher_unavailable'));
  26. }
  27. //套餐状态
  28. $this->quotastate_arr = array(
  29. 'activity' => array(1, lang('voucher_quotastate_activity')),
  30. 'cancel' => array(2, lang('voucher_quotastate_cancel')),
  31. 'expire' => array(3, lang('voucher_quotastate_expire'))
  32. );
  33. //代金券模板状态
  34. $this->templatestate_arr = array(
  35. 'usable' => array(1, lang('voucher_templatestate_usable')),
  36. 'disabled' => array(2, lang('voucher_templatestate_disabled'))
  37. );
  38. }
  39. /**
  40. * @api {POST} api/Sellervoucher/templatelist 代金券列表
  41. * @apiVersion 1.0.0
  42. * @apiGroup Sellervoucher
  43. *
  44. * @apiHeader {String} X-DS-KEY 卖家授权token
  45. *
  46. * @apiParam {String} txt_keyword 关键词
  47. * @apiParam {String} select_state 状态 1:有效 2:失效
  48. * @apiParam {String} txt_startdate 有效期开始时间 YYYY-MM-DD
  49. * @apiParam {String} txt_enddate 有效期结束时间 YYYY-MM-DD
  50. * @apiParam {String} page 页码
  51. * @apiParam {String} pagesize 每页显示数量
  52. *
  53. * @apiSuccess {String} code 返回码,10000为成功
  54. * @apiSuccess {String} message 返回消息
  55. * @apiSuccess {Object} result 返回数据
  56. * @apiSuccess {Object} result.current_quota 代金券活动信息 (返回字段参考voucherquota)
  57. * @apiSuccess {String} result.promotion_voucher_price 购买代金劵活动所需费用
  58. * @apiSuccess {String} result.voucher_storetimes_limit 每月最多可以发布的代金劵促销活动数量
  59. * @apiSuccess {Object} result.vouchertemplate_list 代金券模板列表 (返回字段参考vouchertemplate)
  60. * @apiSuccess {Int} result.page_total 总页数
  61. * @apiSuccess {Boolean} result.hasmore 是否有更多 true是false否
  62. */
  63. public function templatelist()
  64. {
  65. //检查过期的代金券模板状态设为失效
  66. $this->check_voucher_template_expire();
  67. $voucher_model = model('voucher');
  68. $isPlatformStore = false;
  69. $current_quota = false;
  70. if (!$this->store_info['is_platform_store']) {
  71. //查询是否存在可用套餐
  72. $current_quota = $voucher_model->getVoucherquotaCurrent($this->store_info['store_id']);
  73. }
  74. //查询列表
  75. $param = array();
  76. $param[] = array('vouchertemplate_store_id', '=', $this->store_info['store_id']);
  77. if (trim(input('param.txt_keyword'))) {
  78. $param[] = array('vouchertemplate_title', 'like', '%' . trim(input('param.txt_keyword')) . '%');
  79. }
  80. $select_state = intval(input('param.select_state'));
  81. if ($select_state) {
  82. $param[] = array('vouchertemplate_state', '=', $select_state);
  83. }
  84. if (input('param.txt_startdate')) {
  85. $param[] = array('vouchertemplate_enddate', '>=', strtotime(input('param.txt_startdate')));
  86. }
  87. if (input('param.txt_enddate')) {
  88. $param[] = array('vouchertemplate_startdate', '<=', strtotime(input('param.txt_enddate')));
  89. }
  90. $vouchertemplate_list_ = Db::name('vouchertemplate')->where($param)->order('vouchertemplate_id desc')->paginate(['list_rows' => 10, 'query' => request()->param()], false);
  91. $vouchertemplate_list = $vouchertemplate_list_->items();
  92. foreach ($vouchertemplate_list as $key => $val) {
  93. if (!$val['vouchertemplate_customimg']) {
  94. $vouchertemplate_list[$key]['vouchertemplate_customimg'] = ds_get_pic(ATTACH_COMMON, config('ds_config.default_goods_image'));
  95. } else {
  96. $vouchertemplate_list[$key]['vouchertemplate_customimg'] = ds_get_pic(ATTACH_VOUCHER . DIRECTORY_SEPARATOR . $this->store_info['store_id'], $val['vouchertemplate_customimg']);
  97. }
  98. }
  99. $result = array_merge(array('vouchertemplate_list' => $vouchertemplate_list, 'current_quota' => $current_quota, 'promotion_voucher_price' => config('ds_config.promotion_voucher_price'), 'voucher_storetimes_limit' => config('ds_config.voucher_storetimes_limit')), mobile_page($vouchertemplate_list_));
  100. ds_json_encode(10000, '', $result);
  101. }
  102. /**
  103. * @api {POST} api/Sellervoucher/quotaadd 购买套餐
  104. * @apiVersion 1.0.0
  105. * @apiGroup Sellervoucher
  106. *
  107. * @apiHeader {String} X-DS-KEY 卖家授权token
  108. *
  109. * @apiParam {Int} quota_quantity 购买数量
  110. *
  111. * @apiSuccess {String} code 返回码,10000为成功
  112. * @apiSuccess {String} message 返回消息
  113. * @apiSuccess {Object} result 返回数据
  114. * @apiSuccess {Int} result.voucherquota_endtime 代金券活动结束时间
  115. */
  116. public function quotaadd()
  117. {
  118. if (intval(config('ds_config.promotion_voucher_price')) == 0) {
  119. ds_json_encode(10001, lang('param_error'));
  120. }
  121. $quota_quantity = intval(input('post.quota_quantity'));
  122. if ($quota_quantity <= 0 || $quota_quantity > 12) {
  123. ds_json_encode(10001, lang('voucher_apply_num_error'));
  124. }
  125. //获取当前价格
  126. $current_price = intval(config('ds_config.promotion_voucher_price'));
  127. $voucher_model = model('voucher');
  128. //获取该用户已有套餐
  129. $current_quota = $voucher_model->getVoucherquotaCurrent($this->store_info['store_id']);
  130. $quota_add_time = 86400 * 30 * $quota_quantity;
  131. if (empty($current_quota)) {
  132. //生成套餐
  133. $param = array();
  134. $param['voucherquota_memberid'] = $this->seller_info['member_id'];
  135. $param['voucherquota_membername'] = $this->seller_info['seller_name'];
  136. $param['voucherquota_storeid'] = $this->store_info['store_id'];
  137. $param['voucherquota_storename'] = $this->store_info['store_name'];
  138. $param['voucherquota_starttime'] = TIMESTAMP;
  139. $param['voucherquota_endtime'] = TIMESTAMP + $quota_add_time;
  140. $param['voucherquota_state'] = 1;
  141. $reault = Db::name('voucherquota')->insert($param);
  142. } else {
  143. $param = array();
  144. $param['voucherquota_endtime'] = $current_quota['voucherquota_endtime'] + $quota_add_time;
  145. $reault = Db::name('voucherquota')->where(array('voucherquota_id' => $current_quota['voucherquota_id']))->update($param);
  146. }
  147. //记录店铺费用
  148. $this->recordStorecost($current_price * $quota_quantity, lang('buy_voucher_package'));
  149. $this->recordSellerlog(lang('buy') . $quota_quantity . lang('voucher_plan') . $current_price . lang('ds_yuan'));
  150. if ($reault) {
  151. ds_json_encode(10000, lang('voucher_apply_buy_succ'), array('voucherquota_endtime' => $param['voucherquota_endtime']));
  152. } else {
  153. ds_json_encode(10001, lang('ds_common_op_fail'));
  154. }
  155. }
  156. /**
  157. * @api {POST} api/Sellervoucher/templateadd 代金券模版添加
  158. * @apiVersion 1.0.0
  159. * @apiGroup Sellervoucher
  160. *
  161. * @apiHeader {String} X-DS-KEY 卖家授权token
  162. *
  163. * @apiParam {String} txt_template_title 代金券模版名称
  164. * @apiParam {String} txt_template_total 代金券总数
  165. * @apiParam {String} select_template_price 面额
  166. * @apiParam {String} txt_template_limit 订单限额
  167. * @apiParam {String} txt_template_describe 代金券模版描述
  168. * @apiParam {String} txt_template_enddate 有效期结束时间 YYYY-MM-DD
  169. * @apiParam {String} storeclass_id 所属店铺分类ID
  170. * @apiParam {String} eachlimit 每人限领张数
  171. *
  172. * @apiSuccess {String} code 返回码,10000为成功
  173. * @apiSuccess {String} message 返回消息
  174. * @apiSuccess {Object} result 返回数据
  175. */
  176. public function templateadd()
  177. {
  178. $voucher_model = model('voucher');
  179. $isPlatformStore = $this->store_info['is_platform_store'];
  180. $quotainfo = array();
  181. if (!$isPlatformStore) {
  182. //查询当前可以使用的套餐
  183. $quotainfo = $voucher_model->getVoucherquotaCurrent($this->store_info['store_id']);
  184. if (empty($quotainfo)) {
  185. if (intval(config('ds_config.promotion_voucher_price')) == 0) {
  186. $quotainfo = array('voucherquota_id' => 0, 'voucherquota_starttime' => TIMESTAMP, 'voucherquota_endtime' => TIMESTAMP + 86400 * 30); //没有套餐时,最多一个月
  187. } else {
  188. ds_json_encode(10001, lang('voucher_template_quotanull'));
  189. }
  190. }
  191. //查询该套餐下代金券模板列表
  192. $count = Db::name('vouchertemplate')->where(array('vouchertemplate_quotaid' => $quotainfo['voucherquota_id'], 'vouchertemplate_state' => $this->templatestate_arr['usable'][0]))->count();
  193. if ($count >= config('ds_config.voucher_storetimes_limit')) {
  194. $message = sprintf(lang('voucher_template_noresidual'), config('ds_config.voucher_storetimes_limit'));
  195. ds_json_encode(10001, $message);
  196. }
  197. }
  198. $common_data = $this->common_data();
  199. $pricelist = $common_data['pricelist'];
  200. //验证提交的内容面额不能大于限额
  201. $data = [
  202. 'txt_template_title' => input('post.txt_template_title'),
  203. 'txt_template_total' => input('post.txt_template_total'),
  204. 'select_template_price' => input('post.select_template_price'),
  205. 'txt_template_limit' => input('post.txt_template_limit'),
  206. 'txt_template_describe' => input('post.txt_template_describe'),
  207. ];
  208. $sellervoucher_validate = ds_validate('sellervoucher');
  209. if (!$sellervoucher_validate->scene('templateadd')->check($data)) {
  210. ds_json_encode(10001, $sellervoucher_validate->getError());
  211. }
  212. //金额验证
  213. $price = intval(input('post.select_template_price')) > 0 ? intval(input('post.select_template_price')) : 0;
  214. foreach ($pricelist as $k => $v) {
  215. if ($v['voucherprice'] == $price) {
  216. $chooseprice = $v; //取得当前选择的面额记录
  217. }
  218. }
  219. if (empty($chooseprice)) {
  220. ds_json_encode(10001, lang('voucher_template_pricelisterror'));
  221. }
  222. $limit = intval(input('post.txt_template_limit')) > 0 ? intval(input('post.txt_template_limit')) : 0;
  223. if ($price >= $limit) {
  224. ds_json_encode(10001, lang('voucher_template_price_error'));
  225. }
  226. $insert_arr = array();
  227. $insert_arr['vouchertemplate_title'] = trim(input('post.txt_template_title'));
  228. $insert_arr['vouchertemplate_desc'] = trim(input('post.txt_template_describe'));
  229. $insert_arr['vouchertemplate_startdate'] = TIMESTAMP; //默认代金券模板的有效期为当前时间
  230. if (input('post.txt_template_enddate')) {
  231. $enddate = strtotime(input('post.txt_template_enddate'));
  232. if (!$isPlatformStore && $enddate > $quotainfo['voucherquota_endtime']) {
  233. $enddate = $quotainfo['voucherquota_endtime'];
  234. }
  235. $insert_arr['vouchertemplate_enddate'] = $enddate;
  236. } else { //如果没有添加有效期则默认为套餐的结束时间
  237. if ($isPlatformStore)
  238. $insert_arr['vouchertemplate_enddate'] = TIMESTAMP + 2592000; // 自营店 默认30天到期
  239. else
  240. $insert_arr['vouchertemplate_enddate'] = $quotainfo['voucherquota_endtime'];
  241. }
  242. $insert_arr['vouchertemplate_price'] = $price;
  243. $insert_arr['vouchertemplate_limit'] = $limit;
  244. $insert_arr['vouchertemplate_store_id'] = $this->store_info['store_id'];
  245. $insert_arr['vouchertemplate_storename'] = $this->store_info['store_name'];
  246. $insert_arr['vouchertemplate_sc_id'] = intval(input('post.storeclass_id'));
  247. $insert_arr['vouchertemplate_creator_id'] = $this->seller_info['member_id'];
  248. $insert_arr['vouchertemplate_state'] = $this->templatestate_arr['usable'][0];
  249. $insert_arr['vouchertemplate_total'] = intval(input('post.txt_template_total')) > 0 ? intval(input('post.txt_template_total')) : 0;
  250. $insert_arr['vouchertemplate_giveout'] = 0;
  251. $insert_arr['vouchertemplate_used'] = 0;
  252. $insert_arr['vouchertemplate_gettype'] = 1;
  253. $insert_arr['vouchertemplate_adddate'] = TIMESTAMP;
  254. $insert_arr['vouchertemplate_quotaid'] = isset($quotainfo['voucherquota_id']) ? $quotainfo['voucherquota_id'] : 0;
  255. $insert_arr['vouchertemplate_points'] = $chooseprice['voucherprice_defaultpoints'];
  256. $insert_arr['vouchertemplate_eachlimit'] = intval(input('post.eachlimit')) > 0 ? intval(input('post.eachlimit')) : 0;
  257. $insert_arr['vouchertemplate_if_private'] = intval(input('post.vouchertemplate_if_private'));
  258. //自定义图片
  259. if (!empty($_FILES['customimg']['name'])) {
  260. $file_name = $this->store_info['store_id'] . '_' . date('YmdHis') . rand(10000, 99999) . '.png';
  261. $res = ds_upload_pic(ATTACH_VOUCHER . DIRECTORY_SEPARATOR . $this->store_info['store_id'], 'customimg', $file_name);
  262. if ($res['code']) {
  263. $file_name = $res['data']['file_name'];
  264. $insert_arr['vouchertemplate_customimg'] = $file_name;
  265. } else {
  266. ds_json_encode(10001, $res['msg']);
  267. }
  268. }
  269. $rs = Db::name('vouchertemplate')->insert($insert_arr);
  270. if ($rs) {
  271. ds_json_encode(10000, lang('ds_common_op_succ'));
  272. } else {
  273. ds_json_encode(10001, lang('ds_common_op_fail'));
  274. }
  275. }
  276. public function common_data()
  277. {
  278. $voucher_model = model('voucher');
  279. //查询面额列表
  280. $pricelist = Db::name('voucherprice')->order('voucherprice asc')->select()->toArray();
  281. $quotainfo = $voucher_model->getVoucherquotaCurrent($this->store_info['store_id']);
  282. $quotainfo['voucherquota_endtime'] = date('Y-m-d', $quotainfo['voucherquota_endtime']);
  283. if (empty($pricelist)) {
  284. ds_json_encode(10001, lang('voucher_template_pricelisterror'));
  285. }
  286. return array('pricelist' => $pricelist, 'quotainfo' => $quotainfo, 'voucher_buyertimes_limit' => config('ds_config.voucher_buyertimes_limit'));
  287. }
  288. /**
  289. * @api {POST} api/Sellervoucher/get_common_data 代金券新增/编辑公共数据
  290. * @apiVersion 1.0.0
  291. * @apiGroup Sellervoucher
  292. *
  293. * @apiHeader {String} X-DS-KEY 卖家授权token
  294. *
  295. * @apiSuccess {String} code 返回码,10000为成功
  296. * @apiSuccess {String} message 返回消息
  297. * @apiSuccess {Object} result 返回数据
  298. * @apiSuccess {Object[]} result.pricelist 面额列表
  299. * @apiSuccess {Object[]} result.pricelist.voucherprice 面额
  300. * @apiSuccess {Object[]} result.pricelist.voucherprice_defaultpoints 兑换积分
  301. * @apiSuccess {Object[]} result.pricelist.voucherprice_describe 描述
  302. * @apiSuccess {Object[]} result.pricelist.voucherprice_id 面额ID
  303. * @apiSuccess {String} result.voucher_buyertimes_limit 买家最多只能拥有同一个店铺尚未消费抵用的店铺代金券最大数量
  304. */
  305. public function get_common_data()
  306. {
  307. $common_data = $this->common_data();
  308. ds_json_encode(10000, '', $common_data);
  309. }
  310. /**
  311. * @api {POST} api/Sellervoucher/templateadd 代金券模版编辑
  312. * @apiVersion 1.0.0
  313. * @apiGroup Sellervoucher
  314. *
  315. * @apiHeader {String} X-DS-KEY 卖家授权token
  316. *
  317. * @apiParam {Int} tid 代金券模版ID
  318. * @apiParam {String} txt_template_title 代金券模版名称
  319. * @apiParam {String} txt_template_total 代金券总数
  320. * @apiParam {String} select_template_price 面额
  321. * @apiParam {String} txt_template_limit 订单限额
  322. * @apiParam {String} txt_template_describe 代金券模版描述
  323. * @apiParam {String} txt_template_enddate 有效期结束时间 YYYY-MM-DD
  324. * @apiParam {String} storeclass_id 所属店铺分类ID
  325. * @apiParam {String} eachlimit 每人限领张数
  326. * @apiParam {Int} tstate 代金券模版状态 1:有效 2:失效
  327. *
  328. * @apiSuccess {String} code 返回码,10000为成功
  329. * @apiSuccess {String} message 返回消息
  330. * @apiSuccess {Object} result 返回数据
  331. */
  332. public function templateedit()
  333. {
  334. $t_id = intval(input('param.tid'));
  335. if ($t_id <= 0) {
  336. ds_json_encode(10001, lang('param_error'));
  337. }
  338. //查询模板信息
  339. $param = array();
  340. $param[] = array('vouchertemplate_id', '=', $t_id);
  341. $param[] = array('vouchertemplate_store_id', '=', $this->store_info['store_id']);
  342. $param[] = array('vouchertemplate_state', '=', $this->templatestate_arr['usable'][0]);
  343. $param[] = array('vouchertemplate_giveout', '<=', '0');
  344. $param[] = array('vouchertemplate_enddate', '>', TIMESTAMP);
  345. $t_info = Db::name('vouchertemplate')->where($param)->find();
  346. if (empty($t_info)) {
  347. ds_json_encode(10001, lang('param_error'));
  348. }
  349. $isPlatformStore = $this->store_info['is_platform_store'];
  350. View::assign('isPlatformStore', $isPlatformStore);
  351. $quotainfo = array();
  352. if (!$isPlatformStore) {
  353. //查询套餐信息
  354. $quotainfo = Db::name('voucherquota')->where(array(
  355. 'voucherquota_id' => $t_info['vouchertemplate_quotaid'],
  356. 'voucherquota_storeid' => $this->store_info['store_id']
  357. ))->find();
  358. if (empty($quotainfo)) {
  359. ds_json_encode(10001, lang('voucher_template_quotanull'));
  360. }
  361. }
  362. $common_data = $this->common_data();
  363. $pricelist = $common_data['pricelist'];
  364. //验证提交的内容面额不能大于限额
  365. $data = [
  366. 'txt_template_title' => input('post.txt_template_title'),
  367. 'txt_template_total' => input('post.txt_template_total'),
  368. 'select_template_price' => input('post.select_template_price'),
  369. 'txt_template_limit' => input('post.txt_template_limit'),
  370. 'txt_template_describe' => input('post.txt_template_describe'),
  371. ];
  372. $sellervoucher_validate = ds_validate('sellervoucher');
  373. if (!$sellervoucher_validate->scene('templateedit')->check($data)) {
  374. ds_json_encode(10001, $sellervoucher_validate->getError());
  375. }
  376. //金额验证
  377. $price = intval(input('post.select_template_price')) > 0 ? intval(input('post.select_template_price')) : 0;
  378. foreach ($pricelist as $k => $v) {
  379. if ($v['voucherprice'] == $price) {
  380. $chooseprice = $v; //取得当前选择的面额记录
  381. }
  382. }
  383. if (empty($chooseprice)) {
  384. ds_json_encode(10001, lang('voucher_template_pricelisterror'));
  385. }
  386. $limit = intval(input('post.txt_template_limit')) > 0 ? intval(input('post.txt_template_limit')) : 0;
  387. if ($price >= $limit) {
  388. ds_json_encode(10001, lang('voucher_template_price_error'));
  389. }
  390. $update_arr = array();
  391. $update_arr['vouchertemplate_title'] = trim(input('post.txt_template_title'));
  392. $update_arr['vouchertemplate_desc'] = trim(input('post.txt_template_describe'));
  393. if (input('post.txt_template_enddate')) {
  394. $enddate = strtotime(input('post.txt_template_enddate'));
  395. if (!$isPlatformStore && $enddate > $quotainfo['voucherquota_endtime']) {
  396. $enddate = $quotainfo['voucherquota_endtime'];
  397. }
  398. $update_arr['vouchertemplate_enddate'] = $enddate;
  399. } else { //如果没有添加有效期则默认为套餐的结束时间
  400. if ($isPlatformStore)
  401. $update_arr['vouchertemplate_enddate'] = TIMESTAMP + 2592000; // 自营店 默认30天到期
  402. else
  403. $update_arr['vouchertemplate_enddate'] = $quotainfo['voucherquota_endtime'];
  404. }
  405. $update_arr['vouchertemplate_price'] = $price;
  406. $update_arr['vouchertemplate_limit'] = $limit;
  407. $update_arr['vouchertemplate_sc_id'] = intval(input('post.storeclass_id'));
  408. $update_arr['vouchertemplate_state'] = intval(input('post.tstate')) == $this->templatestate_arr['usable'][0] ? $this->templatestate_arr['usable'][0] : $this->templatestate_arr['disabled'][0];
  409. $update_arr['vouchertemplate_total'] = intval(input('post.txt_template_total')) > 0 ? intval(input('post.txt_template_total')) : 0;
  410. $update_arr['vouchertemplate_points'] = $chooseprice['voucherprice_defaultpoints'];
  411. $update_arr['vouchertemplate_eachlimit'] = intval(input('post.eachlimit')) > 0 ? intval(input('post.eachlimit')) : 0;
  412. $update_arr['vouchertemplate_if_private'] = intval(input('post.vouchertemplate_if_private'));
  413. //自定义图片
  414. if (!empty($_FILES['customimg']['name'])) {
  415. $file_name = $this->store_info['store_id'] . '_' . date('YmdHis') . rand(10000, 99999);
  416. $res = ds_upload_pic(ATTACH_VOUCHER . DIRECTORY_SEPARATOR . $this->store_info['store_id'], 'customimg', $file_name);
  417. if ($res['code']) {
  418. $file_name = $res['data']['file_name'];
  419. //删除原图
  420. if (!empty($t_info['vouchertemplate_customimg'])) { //如果模板存在,则删除原模板图片
  421. @unlink(BASE_UPLOAD_PATH . DIRECTORY_SEPARATOR . ATTACH_VOUCHER . DIRECTORY_SEPARATOR . $this->store_info['store_id'] . DIRECTORY_SEPARATOR . $t_info['vouchertemplate_customimg']);
  422. }
  423. $update_arr['vouchertemplate_customimg'] = $file_name;
  424. } else {
  425. ds_json_encode(10001, $res['msg']);
  426. }
  427. }
  428. $rs = Db::name('vouchertemplate')->where(array('vouchertemplate_id' => $t_info['vouchertemplate_id']))->update($update_arr);
  429. if ($rs) {
  430. ds_json_encode(10000, lang('ds_common_op_succ'));
  431. } else {
  432. ds_json_encode(10001, lang('ds_common_op_fail'));
  433. }
  434. }
  435. /**
  436. * @api {POST} api/Sellervoucher/templatedel 删除代金券
  437. * @apiVersion 1.0.0
  438. * @apiGroup Sellervoucher
  439. *
  440. * @apiHeader {String} X-DS-KEY 卖家授权token
  441. *
  442. * @apiParam {Int} tid 代金券模版ID
  443. *
  444. * @apiSuccess {String} code 返回码,10000为成功
  445. * @apiSuccess {String} message 返回消息
  446. * @apiSuccess {Object} result 返回数据
  447. */
  448. public function templatedel()
  449. {
  450. $t_id = intval(input('param.tid'));
  451. if ($t_id <= 0) {
  452. ds_json_encode(10001, lang('param_error'));
  453. }
  454. //查询模板信息
  455. $param = array();
  456. $param[] = array('vouchertemplate_id', '=', $t_id);
  457. $param[] = array('vouchertemplate_store_id', '=', $this->store_info['store_id']);
  458. $param[] = array('vouchertemplate_giveout', '<=', '0'); //会员没领取过代金券才可删除
  459. $t_info = Db::name('vouchertemplate')->where($param)->find();
  460. if (empty($t_info)) {
  461. ds_json_encode(10001, lang('param_error'));
  462. }
  463. $rs = Db::name('vouchertemplate')->where(array('vouchertemplate_id' => $t_info['vouchertemplate_id']))->delete();
  464. if ($rs) {
  465. //删除自定义的图片
  466. if (trim($t_info['vouchertemplate_customimg'])) {
  467. @unlink(BASE_UPLOAD_PATH . DIRECTORY_SEPARATOR . ATTACH_VOUCHER . DIRECTORY_SEPARATOR . $this->store_info['store_id'] . DIRECTORY_SEPARATOR . $t_info['vouchertemplate_customimg']);
  468. }
  469. ds_json_encode(10000, lang('ds_common_del_succ'));
  470. } else {
  471. ds_json_encode(10001, lang('ds_common_del_fail'));
  472. }
  473. }
  474. /**
  475. * 查看代金券详细
  476. */
  477. public function templateinfo()
  478. {
  479. $t_id = intval(input('param.tid'));
  480. if ($t_id <= 0) {
  481. ds_json_encode(10001, lang('param_error'), 'Sellervoucher/templatelist');
  482. }
  483. //查询模板信息
  484. $param = array();
  485. $param['vouchertemplate_id'] = $t_id;
  486. $param['vouchertemplate_store_id'] = $this->store_info['store_id'];
  487. $t_info = Db::name('vouchertemplate')->where($param)->find();
  488. ds_json_encode(10000, lang('ds_common_del_succ'), array('t_info' => $t_info));
  489. }
  490. /*
  491. * 把代金券模版设为失效
  492. */
  493. private function check_voucher_template_expire($voucher_template_id = '')
  494. {
  495. $where_array = array();
  496. if (empty($voucher_template_id)) {
  497. $where_array[] = array('vouchertemplate_enddate', '<', TIMESTAMP);
  498. } else {
  499. $where_array[] = array('vouchertemplate_id', '=', $voucher_template_id);
  500. }
  501. $where_array[] = array('vouchertemplate_state', '=', $this->templatestate_arr['usable'][0]);
  502. Db::name('vouchertemplate')->where($where_array)->update(array('vouchertemplate_state' => $this->templatestate_arr['disabled'][0]));
  503. }
  504. }